1

This code compiles OK, but the ComboBox (cbxColors) is empty - not populated from the Data Source (array: COLORS_OF). Uses Data Source is checked in IB.

func numberOfItemsInComboBox() returns the correct result: 5.

func comboBox() is not doing its job.

What am I missing?

EDITED: Now working.

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSComboBoxDelegate, NSComboBoxDataSource {

@IBOutlet weak var window: NSWindow!


func applicationDidFinishLaunching(aNotification: NSNotification) {

    cbxColors.dataSource = self

    numberOfItemsInComboBoxCell(cbxColors)
    comboBoxCell(cbxColors, objectValueForItemAtIndex: 0)
}

func applicationWillTerminate(aNotification: NSNotification) {

}

@IBOutlet weak var cbxColors: NSComboBox!
@IBOutlet weak var txtResult: NSTextField!


@IBAction func actColors(sender: NSComboBox) {
    // display User selected item in 'txtResult'
}

func numberOfItemsInComboBoxCell(aComboBox: NSComboBox) -> Int {
    return(COLORS_OF.count)
}

func comboBoxCell(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
    return(COLORS_OF[index])
}

let COLORS_OF = [ "Blue", "Green", "Purple", "Red", "Yellow" ]
} 
San Lewy
  • 136
  • 1
  • 3
  • 13
  • What do you mean by "not doing it's job"? Is your "cbxColors" IBoutlet correctly hooked and not nil? Why do you have "NSTextFieldCell" IBoutlet instead of "NSTextField" ? – Marek H Oct 06 '15 at 09:46
  • @xhruso00 "cbxColors" IBOutlet is correctly hooked to the ComboBox in IB. I changed "NSTextFieldCell! as suggested. I say "not doing its job" because the ComboBox is not getting populated. – San Lewy Oct 06 '15 at 10:38

1 Answers1

3

You probably forgot to check Uses Data Source or you have to delete datasource connection and reconnect it again (weird bug of Xcode).

enter image description here

Other then that if your outlets are correctly hooked your code works.

enter image description here

Marek H
  • 5,173
  • 3
  • 31
  • 42
  • As I stated, 'Uses Data Source' is checked. – San Lewy Oct 06 '15 at 15:24
  • Does your code stop on breakpoints (numberOfItems?) What OS X do you use? Can you attach sample project (works for me out of box so I will test if your project is OK). – Marek H Oct 06 '15 at 15:31
  • The example now works. I added this statement: cbxColors.dataSource = self and added 'Cell' to the two functions and calls. – San Lewy Oct 06 '15 at 15:59
  • 1
    This means you did not have all outlets hooked; namely datasource ;) Sometimes there is a bug in Xcode so you have to delete connection and reconnect. – Marek H Oct 06 '15 at 16:10
  • Yes, that was the case. When I deleted and reconnect the datasource I didn't need the code I added - it worked as I thought it should have. If you would like to post your comment as an answer, I would be pleased to accept it. Thanks. – San Lewy Oct 06 '15 at 16:38
  • Updated the answer. Hope it helps other – Marek H Oct 06 '15 at 16:54