我有一个非常简单的UI,只有一个NSPopUpButton。其selectedIndex绑定到intViewController.self.my_selection。一旦从UI更改选择(即选择NSPopUpButton的第三项),我就会看到my_selection值发生变化。到目前为止,我想要获得的是相反的方向。我想以编程方式更改my_selection值,并看到NSPopUpButton选择该项目以及我在my_selection中定义的索引。我错误地认为行为是绑定的默认行为...

这就是我现在得到的:

NSPoPUpButton ---> select item at index 2 ----> my_selection becomes equal to 2


这就是我想要实现的(同时保持以前的行为)

my_selection ---> set value to 3----> NSPoPUpButton selected index = 3

最佳答案

没有更多信息(请参阅我的评论),很难确切地知道您在做什么。这是我的工作方式:首先,创建一个简单的类...

// Create a simple class
class Beatle: NSObject {
    convenience init(name: String) {
        self.init()
        self.name = name
    }
    dynamic var name: String?
}


然后,在AppDelegate中创建了一个名为beatles的四项数组:

dynamic var beatles: [Beatle]?

func applicationDidFinishLaunching(aNotification: NSNotification) {

    beatles = [Beatle(name: "John"),
        Beatle(name: "Paul"),
        Beatle(name: "Ringo"),
        Beatle(name: "George")]
}


在Interface Builder中,我进行了设置,以便此数组向弹出窗口提供其内容:

cocoa - 绑定(bind)和NSPopUpButton:更改当前键的值不会更改选择-LMLPHP

此类还具有绑定到弹出按钮的selectedIndex绑定的selectedIndex属性-此属性提供对弹出按钮的选择的读写访问:

// Set the pop-up selection by changing the value of this variable.
// (Make sure you mark it as dynamic.)
dynamic var selectedIndex: Int = 0

关于cocoa - 绑定(bind)和NSPopUpButton:更改当前键的值不会更改选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35917599/

10-11 22:02
查看更多