Xcode 8/Swift 3:简单的UIPicker代码不起作用 [英] Xcode 8 / Swift 3 : Simple UIPicker code not working
问题描述
我有协议:
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
type viewcontroller does not conform to protocol UIPickerViewDataSource
推荐答案
UIPickerViewDataSource
方法numberOfComponentsInPickerView
在Swift 3中被更改,这就是您遇到此错误的原因.
UIPickerViewDataSource
method numberOfComponentsInPickerView
is changed in Swift 3 like this that is the reason you are getting this error.
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
上的 Apple文档.
For more detail read Apple Documentation on UIPickerView
.
注意:您还需要添加_
作为第一个参数标签,就像UIPickerViewDelegate
和didSelectRow
方法中的其他方法一样.
Note: You need to also add _
as first parameter label same like other methods in your UIPickerViewDelegate
method that is titleForRow
and didSelectRow
.
这篇关于Xcode 8/Swift 3:简单的UIPicker代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!