【问题标题】:How to implement the data source for multiple combo boxes in the same view controller如何在同一个视图控制器中实现多个组合框的数据源
【发布时间】:2017-03-20 18:41:54
【问题描述】:

我有 2 个组合框,它们使用与数据源相同的字典。在第一个组合框中将显示键,在第二个组合框中将显示值。

首先我需要一些帮助来实现数据源委托方法;请参阅下面的代码清单。相关的盒子是 floorBox 和 roomsBox。忽略其他 2 个,因为它们将从局部变量或接口中填充。 另外,假设一楼 [key = 0] 我有 3 个房间 [value = 3],我如何取 3 的值并将其变成 roomsBox 内的 3 个项目;这样每次地板改变房间的盒子都有相应数量的物品(如果有意义的话)。我真的很感谢你的帮助,我也谢谢你。

class RoomAndAlarmTypesVC: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {

//MARK: - Properties

private var floorsAndRooms = [String: String]()

@IBOutlet private weak var floorsBox: NSComboBox!

@IBOutlet private weak var roomsBox: NSComboBox!

@IBOutlet private weak var roomType: NSComboBox!

@IBOutlet private weak var alarmType: NSComboBox!

//MARK: - Actions

override func viewDidLoad() {
    super.viewDidLoad()
    floorsAndRooms = representedObject as! [String : String]
    floorsBox.usesDataSource = true
    roomsBox.usesDataSource = true
    roomType.usesDataSource = true
    alarmType.usesDataSource = true

    floorsBox.delegate = self
    roomsBox.delegate = self
    roomType.delegate = self
    alarmType.delegate = self

    floorsBox.dataSource = self
    roomsBox.dataSource = self
    roomType.dataSource = self
    alarmType.dataSource = self
}
@IBAction private func setTheSystem(_ sender: NSButton) {

}

func numberOfItems(in comboBox: NSComboBox) -> Int {
    what do i do here?
}

func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
    same here?
}

}

【问题讨论】:

    标签: swift cocoa swift3 nscombobox


    【解决方案1】:

    根据发送者(您的组合框参数)使用 switch 语句

    例如:

    switch comboBox
    {
        case floorsBox :  return // the data for floors 
        case roomsBox :  return // the data for rooms 
        ...
    }
    

    【讨论】:

    • 感谢您的宝贵时间。你的回答很有道理。但我从 switch 语句中得到一个错误: NSComboBox 类型的值没有成员计数。当我尝试返回项目数时会发生这种情况
    • 我已经完成了。谢谢你。您的解决方案有效。现在我需要弄清楚当盒子里的地板发生变化时如何改变房间盒子里的物品
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多