我有一个非常简单的用户界面NSPopUpButton
。其selectedIndex
绑定到int
值ViewController.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
答案 0 :(得分:1)
如果没有更多信息(请参阅我的评论),很难确切地看出你做错了什么。以下是我如何使用它:首先,创建一个简单的类......
// 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中我进行了设置,以便此数组为弹出窗口提供其内容:
此类还具有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