简体   繁体   English

类型'x'不符合协议'UIPickerViewDataSource'

[英]Type 'x' does not conform to protocol 'UIPickerViewDataSource'

    import UIKit

class FourthViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {


    @IBOutlet weak var picker: UIPickerView!

      var pickerData: [String] = [String]()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.picker.delegate = self
        self.picker.dataSource = self

        pickerData = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: - Delegates and data sources
    //MARK: Data Sources
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    //MARK: Delegates
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[row]
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Error:Type 'FourthViewController' does not conform to protocol 'UIPickerViewDataSource' 错误:类型'FourthViewController'不符合协议'UIPickerViewDataSource'

Just for testing some features but i dont get the problem 只是为了测试一些功能,但我没有得到问题

As you have found, you need to implement numberOfComponents(in:) . 如您numberOfComponents(in:) ,您需要实现numberOfComponents(in:)

Your numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int , needs to be changed to: 你的numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int ,需要改为:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

Also you need to modify your pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 你还需要修改你的pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? :

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}

In Swift 3 many methods are renamed. 在Swift 3中,许多方法都被重命名。 Check the latest reference , and be careful about this . 检查最新的参考 ,并注意这一点 And you better remark Xcode version in your question. 你最好在你的问题中评论Xcode版本。

You must conform data source protocol: 您必须符合数据源协议:

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

Try this code: 试试这段代码:

//MARK: - Delegates and data sources
        //MARK: Data Sources
        public func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }

        public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return pickerData.count
        }

        //MARK: Delegate
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return pickerData[row]
        }

You have to implement all the required methods of UIPickerViewDataSource and UIPickerViewDelegate if you confirming that protocol in swift . 如果在swift确认该协议,则必须实现UIPickerViewDataSourceUIPickerViewDelegate所需的所有方法。 otherwise it will give error at compile time. 否则它会在编译时给出错误。

I got error due to the delegate method 由于委托方法,我收到错误

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 
    {
    if pickerView == stylePickerView {
        return 1        }
    else if pickerView == fontPickerView {
        return 1        }
    return 0
    }

tried this instead and worked 试过这个并且工作了

@available(iOS 2.0, *)
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
    if pickerView == stylePickerView {
        return 1        }
    else if pickerView == fontPickerView {
        return 1        }
    return 0 
}

You are missing this from the delegate: 您在委托中遗漏了这个:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM