gpt4 book ai didi

swift - Xcode 8/swift 3 : Simple UIPicker code not working

转载 作者:IT王子 更新时间:2023-10-29 05:33:15 24 4
gpt4 key购买 nike

我有协议(protocol):

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) {
}

我还是明白了

type viewcontroller does not conform to protocol UIPickerViewDataSource

最佳答案

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) {

}

有关更多详细信息,请阅读 Apple DocumentationUIPickerView 上。

注意:您还需要添加 _ 作为第一个参数标签,就像 UIPickerViewDelegate 方法中的其他方法一样 titleForRow didSelectRow

关于swift - Xcode 8/swift 3 : Simple UIPicker code not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com