Reputation: 16430
I've a really simple UI with a single NSPopUpButton
. Its selectedIndex
is bound to an int
value ViewController.self.my_selection
. As soon as I change the selection from the UI (i.e. a select the third item of the NSPopUpButton) I see that my_selection
value changes. So far so good, what I'm trying to obtain is the opposed direction though. I want to change the my_selection
value programmatically and see the NSPopUpButton
selecting the item a the index that I've defined in my_selection
. I erroneously supposed that behaviour was the default behaviour for bindings...
This is what I'm obtaining now:
NSPoPUpButton ---> select item at index 2 ----> my_selection becomes equal to 2
This is what I want to achieve (keeping also the previous behaviour)
my_selection ---> set value to 3----> NSPoPUpButton selected index = 3
Upvotes: 0
Views: 904
Reputation: 6918
Without a bit more info (see my comment) it's hard to see exactly what you're doing wrong. Here's how I got it working: First, create a simple class...
// Create a simple class
class Beatle: NSObject {
convenience init(name: String) {
self.init()
self.name = name
}
dynamic var name: String?
}
Then, in the AppDelegate
I created a four-item array called beatles
:
dynamic var beatles: [Beatle]?
func applicationDidFinishLaunching(aNotification: NSNotification) {
beatles = [Beatle(name: "John"),
Beatle(name: "Paul"),
Beatle(name: "Ringo"),
Beatle(name: "George")]
}
In Interface Builder I set things up so that this array provides the pop-up with its content:
This class also has a selectedIndex
property that is bound to the pop-up button's selectedIndex
binding - this property provides read-write access to the pop-up button's selection:
// Set the pop-up selection by changing the value of this variable.
// (Make sure you mark it as dynamic.)
dynamic var selectedIndex: Int = 0
Upvotes: 2