简体   繁体   中英

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'

Just for testing some features but i dont get the problem

As you have found, you need to implement numberOfComponents(in:) .

Your numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int , needs to be changed to:

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

Also you need to modify your 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. Check the latest reference , and be careful about this . And you better remark Xcode version in your question.

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 . 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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