4

I am new to OS X app development. I manage to built the NSComboBox (Selectable, not editable), I can get it indexOfSelectedItem on action button click, working fine.

How to detect the the value on change? When user change their selection, what kind of function I shall use to detect the new selected index?

I tried to use the NSNotification but it didn't pass the new change value, always is the default value when load. It is because I place the postNotificationName in wrong place or there are other method should use to get the value on change?

I tried searching the net, video, tutorial but mostly written for Objective-C. I can't find any answer for this in SWIFT.

import Cocoa

class NewProjectSetup: NSViewController {

    let comboxRouterValue: [String] = ["No","Yes"]

    @IBOutlet weak var projNewRouter: NSComboBox!

    @IBAction func btnAddNewProject(sender: AnyObject) {
        let comBoxID = projNewRouter.indexOfSelectedItem
        print(“Combo Box ID is: \(comBoxID)”)
    }

    @IBAction func btnCancel(sender: AnyObject) {
        self.dismissViewController(self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        addComboxValue(comboxRouterValue,myObj:projNewRouter)
        self.projNewRouter.selectItemAtIndex(0)

        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(
        self,
        selector: “testNotication:”,
        name:"NotificationIdentifier",
        object: nil) 

        NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: projNewRouter.indexOfSelectedItem)
}

func testNotication(notification: NSNotification){
    print("Found Combo ID  \(notification.object)")
}

func addComboxValue(myVal:[String],myObj:AnyObject){
    let myValno: Int = myVal.count
    for var i = 0; i < myValno; ++i{
        myObj.addItemWithObjectValue(myVal[i])
    }
}

}

David Evony
  • 273
  • 3
  • 15
  • You need to implement NSComboBoxDelegate and use the method `comboBoxSelectionDidChange()` to know pop-up list selection changed – Leo Dabus Jan 22 '16 at 02:08
  • BTW you should get used to the new Swift syntax for loops. For var loops won't be available anymore in Swift3 https://github.com/apple/swift-evolution/blob/master/proposals/0007-remove-c-style-for-loops.md – Leo Dabus Jan 22 '16 at 02:14
  • Ohh, yes, I will need to pick up the for-in style. – David Evony Jan 22 '16 at 02:50

1 Answers1

9

You need to define a delegate for the combobox that implements the NSComboBoxDelegate protocol, and then use the comboBoxSelectionDidChange(_:) method.

The easiest method is for your NewProjectSetup class to implement the delegate, as in:

class NewProjectSetup: NSViewController, NSComboBoxDelegate { ... etc

Then in viewDidLoad, also include:

self.projNewRouter.delegate = self
// self (ie. NewProjectSetup) implements NSComboBoxDelegate 

And then you can pick up the change in:

func comboBoxSelectionDidChange(notification: NSNotification) {
    print("Woohoo, it changed")
}
Michael
  • 8,891
  • 3
  • 29
  • 42
  • Excellent! I follow your instruction. It is working now. I removedthe "let notfication = Notification....." and also removed the "NSNotificationCenter.defaultCenter().postNotificationName". The output i use print("Woohoo, it changed \(self.projNewRouter.indexOfSelectedItem)"). It prefect now. Thank you – David Evony Jan 22 '16 at 02:52
  • method header has changed to: `func comboBoxSelectionDidChange(_ notification: Notification)` – Paul Stevenson Nov 14 '22 at 01:03