Xcode 8/Swift 3:简单的UIPicker代码不起作用 [英] Xcode 8 / Swift 3 : Simple UIPicker code not working

查看:103
本文介绍了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) {
    }

我还是

类型的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.

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆