一尘不染

Xcode 8 / Swift 3:简单的UIPicker代码不起作用

swift

我有协议:

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

我有数据:

let muteForPickerData = ["minute(s)","hour(s)"]

viewDidLoad我有:

muteForPicker.delegate = self
muteForPicker.dataSource = self

然后我有所需的方法:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
            return 1
        }

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return muteForPickerData.count
        }

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return muteForPickerData[row]
        }

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    }

我还是

类型的viewcontroller不符合协议UIPickerViewDataSource


阅读 262

收藏
2020-07-07

共1个答案

一尘不染

UIPickerViewDataSource``numberOfComponentsInPickerView像这样在Swift
3中更改了方法,这就是您收到此错误的原因。

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return muteForPickerData.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return muteForPickerData[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

}

欲了解更多详细阅读苹果文档UIPickerView

注意:
您还需要添加_的第一个参数标签同样喜欢你的其他方法UIPickerViewDelegate方法是titleForRowdidSelectRow

2020-07-07