本文介绍了Xcode 8/Swift 3:简单的 UIPicker 代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有协议:
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) {
}
我还是明白了
类型视图控制器不符合协议 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
方法中的其他方法相同,即 titleForRow
和 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 代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!