0

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" ]
} 
4

1 回答 1

1

您可能忘记检查 Uses Data Source 或者您必须删除数据源连接并重新连接(Xcode 的奇怪错误)。

在此处输入图像描述

除此之外,如果您的网点正确连接,您的代码就可以工作。

在此处输入图像描述

于 2015-10-06T14:32:20.837 回答