简体   繁体   中英

UIPickerView on Swift 3

I have error with using UIPickerView

SelectViewController.swift:35:10: Method 'pickerView(pickerView:numberOfRowsInComponent:)' has different argument names from those required by protocol 'UIPickerViewDataSource' ('pickerView(_:numberOfRowsInComponent:)')

I set UIPickerView on Storyboard and attached this to the songPicker variable.

and then I think I integrated the functions necessary though, it showed error like this.

I found out that structure of picker view is changed on the version of swift.

However can't find the correct answer yet.

My swift is 3.1

Does anyone help me?

class SelectViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

    @IBOutlet weak var songPicker: UIPickerView!

    let songNames = ["test","test2"]

    override func viewDidLoad(){
            songPicker.delegate = self
          songPicker.dataSource = self

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


    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return songNames.count
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int) -> String? {
        return songNames[row]
    }
    override func didReceiveMemoryWarning() {

    }
}

try this code

class ElibraryViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource {

@IBOutlet weak var txtTitle: UITextField!
 @IBOutlet weak var Picker: UIPickerView!
let songNames = ["test","test2"]

override func viewDidLoad()
    {

        super.viewDidLoad()


            }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {


            return songNames[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {



        let obj = songNames[row]
        txtTitle.text = obj



    }

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

        return 1
    }

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



        return songNames.count



    }
override func didReceiveMemoryWarning() {

    }
}

Use this method, see the use of _ before pickerview. That is the only problem

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

    }

UIPickerViewDataSource is changes in swift 3 and above.

Updated syntax:-

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

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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

}

Have a look at apple documentation ( SO reference).

我建议您使用IQDropDownTextField- 单击此处

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