在objc中,我可以获取NSPopUpButton的selectedItem,titleOfSelectedItem和indexOfSelectedItem:
- (IBAction) myPopUpButton: (id)sender {
NSLog( [sender selectedItem] ); // works
NSLog( [sender titleOfSelectedItem] ); // works
NSLog( [sender indexOfSelectedItem] ); // works
}
在swift中,我可以获取selectedItem和titleOfSelectedItem,但不能获取NSPopUpButton的indexOfSelectedItem:
@IBAction func myPopUpButton(sender: AnyObject) {
println(sender.selectedItem) // works
println(sender.titleOfSelectedItem) // works
println(sender.indexOfSelectedItem) // does not work
}
我错过了什么?
答案 0 :(得分:4)
它看起来像一个bug。如果您将其投放到NSPopUpButton
,它会按预期运行:
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)