我有协议:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
我有数据:
let muteForPickerData = ["minute(s)","hour(s)"]
在viewDidLoad我有:
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
UIPickerViewDataSource``numberOfComponentsInPickerView像这样在Swift 3中更改了方法,这就是您收到此错误的原因。
UIPickerViewDataSource``numberOfComponentsInPickerView
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。
UIPickerView
注意: 您还需要添加_的第一个参数标签同样喜欢你的其他方法UIPickerViewDelegate方法是titleForRow和didSelectRow。
_
UIPickerViewDelegate
titleForRow
didSelectRow