【Swift Mac开发】NSComboBox的使用

  1、添加数据源

var titles = ["30分","1小时","4小时","日K","周K","月K","年K"]

  2、创建comboBox

        periodComboBox = NSComboBox()
        periodComboBox.usesDataSource = true
        periodComboBox.completes = true
        periodComboBox.delegate = self
        periodComboBox.dataSource = self
        periodComboBox.stringValue = "更多"
        self.addSubview(periodComboBox)

  3、实现代理方法

extension BMKliePeriodView : NSComboBoxDelegate , NSComboBoxDataSource {
    func numberOfItems(in comboBox: NSComboBox) -> Int {
        return titles.count
    }
    
    func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
        return titles[index]
    }


    func comboBoxSelectionDidChange(_ notification: Notification) {
         
        if let comboBox = notification.object as? NSComboBox {
            print (titles[comboBox.indexOfSelectedItem])
        }
     
    }
}

   效果图:

 

posted @ 2021-05-13 19:08  Mr·Xu  阅读(372)  评论(0编辑  收藏  举报