Reputation: 12433
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() {
}
}
Upvotes: 2
Views: 1042
Reputation: 1448
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() {
}
}
Upvotes: 2
Reputation: 8504
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).
Upvotes: 1
Reputation: 12594
Use this method, see the use of _
before pickerview. That is the only problem
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
}
Upvotes: 1