3

In objc, I can get the selectedItem, titleOfSelectedItem and indexOfSelectedItem of an NSPopUpButton:

- (IBAction) myPopUpButton: (id)sender {
    NSLog( [sender selectedItem] ); // works
    NSLog( [sender titleOfSelectedItem] ); // works
    NSLog( [sender indexOfSelectedItem] ); // works
}

In swift, I can get the selectedItem and titleOfSelectedItem but not the indexOfSelectedItem of an NSPopUpButton:

@IBAction func myPopUpButton(sender: AnyObject) {
    println(sender.selectedItem) // works
    println(sender.titleOfSelectedItem) // works
    println(sender.indexOfSelectedItem) // does not work
}

What am I missing?

user1890913
  • 107
  • 6
  • 1
    Works for me just fine using Xcode6-Beta7. What do you mean with "does not work", are you getting a compiler error or a wrong index? – csch Sep 08 '14 at 09:32
  • @csch - compiler error. Moved to Beta7 and works now. Thanks! – user1890913 Sep 08 '14 at 17:15

1 Answers1

4

It looks like a bug. If you cast it to NSPopUpButton, it works as expected:

func myPopUpButton(sender: AnyObject) {
    if let pub = sender as? NSPopUpButton {
        println(pub.selectedItem) // "<NSMenuItem: 0x7fd75b63cec0 1>"
        println(pub.titleOfSelectedItem) // "1"
        println(sender.indexOfSelectedItem) // "(Function)" !!! What???
        println(pub.indexOfSelectedItem) // "0"
    }
}

let b = NSPopUpButton()
b.addItemsWithTitles(["1", "2"])
b.selectItemAtIndex(0)
myPopUpButton(b)
Grimxn
  • 22,115
  • 10
  • 72
  • 85