3

I have the following code written in SWIFT for OS X App, the code is working fine (NSComboBox are select able only, not editable)

I have these two IBOutlet projNewProjType and projNewRouter, when I change the the selection of either of the NSComboBox, I can see the correct selected Index value and String value but how to check that the returned Index value is from projNewProjType NOT projNewRouter in the comboBoxSelectionDidChange()

import Cocoa
class NewProjectSetup: NSViewController, NSComboBoxDelegate {
  let comboxProjValue: [String] = [“No”,”Yes”]
  let comboxRouterValue: [String] = ["No","Yes"]

  @IBOutlet weak var projNewProjType: NSComboBox!
  @IBOutlet weak var projNewRouter: NSComboBox!


  @IBAction func btnAddNewProject(sender: AnyObject) {
    print(“Add New Button Pressed!”)
  }

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

  override func viewDidLoad() {
    super.viewDidLoad()
    addComboxValue(comboxProjValue,projNewProjType)
    addComboxValue(comboxRouterValue,projNewRouter)

    self.projNewProjType.selectItemAtIndex(0)
    self.projNewRouter.selectItemAtIndex(0)

    self.projNewProjType.delegate = self
    self.projNewRouter.delegate = self
  }


  func comboBoxSelectionDidChange(notification: NSNotification) {
    let comboBox: NSComboBox = (notification.object as? NSComboBox)!

    print("comboBox comboBox: \(comboBox)")
    /* This printed “<NSComboBox: 0x6000001e1a00>”*/

    print("comboBox objectValueOfSelectedItem: \(comboBox.objectValueOfSelectedItem)")
    /* This printed the correct selected String value */

    print("comboBox indexOfSelectedItem: \(comboBox.indexOfSelectedItem)")
    /* This printed the correct selected Index value */        
  }

  /* Add value to Combo box */
  func addComboxValue(myVal:[String],myObj:AnyObject){
    let myValno: Int = myVal.count
    for i in 0..<myValno{
        myObj.addItemWithObjectValue(myVal[i])
    }
  }
}
David Evony
  • 273
  • 3
  • 15

2 Answers2

3

You already know the addresses of your two NSComboBox outlets and you know the address of which NSComboBox caused that notification to trigger, so why not do something like:

func comboBoxSelectionDidChange(notification: NSNotification) {
    let comboBox: NSComboBox = (notification.object as? NSComboBox)!

    if comboBox == self.projNewProjType
    {
        print("selection changed via self.projNewProjType")
    }
    if comboBox == self.projNewRouter
    {
        print("selection changed via self.projNewRouter")
    }
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
2

You can set identifiers to your NSComboBoxes in IB. Select your combo box and choose identity inspector and name identifier. Then you are able to do like this:

if comboBox.identifier == "someIdentifier" {
    // Do something
}
Prontto
  • 1,671
  • 11
  • 21
  • Ooop! I'm bad. I set my identifier to wrong section under the Accessibility section, that's explain why I see strange string [_NS: 179] when i tried to print .identifier. After I correct it and put it under the Identity section, now I can use the .identifier to check the selector. Thank you – David Evony Jan 22 '16 at 14:13