2016-03-10 10 views
0

我有一个非常简单的用户界面,只有一个NSPopUpButton。其selectedIndex绑定到intViewController.self.my_selection。只要我从UI中更改选择(即选择NSPopUpButton的第三项),我就会看到my_selection值发生了变化。到目前为止这么好,我试图获得的是相反的方向。我想以编程方式更改my_selection值,并查看NSPopUpButton选择项目作为我在my_selection中定义的索引。我错误地假设的行为是对绑定的默认行为......绑定和NSPopUpButton:更改当前键的值不会改变选择

这就是我现在获得:

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

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

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

你能告诉我们的实际代码?特别是,你如何在你的''ViewController''类中定义''my_selection''? –

回答

0

没有更多的信息(请参阅我的评论)很难确切地看到你做错了什么。以下是我得到它的工作:首先,创建一个简单的类...

// 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我设置事情让这个数组提供弹出其内容:

enter image description here

这个类也有一个绑定到弹出按钮的属性绑定 - 这个属性提供读写访问弹出按钮的选择:

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