Xcode 8/Swift 3: Simple Uipicker Code Not Working

Xcode 8 / Swift 3 : Simple UIPicker code not working

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) {

}

For more detail read Apple Documentation on UIPickerView.

Note: You need to also add _ as first parameter label same like other methods in your UIPickerViewDelegate method that is titleForRow and didSelectRow.

UIPickerView on Swift 3

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

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

}

PickerView: Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'

The method signature for numberOfComponents(in:) changed from iOS 9.3 to iOS 10. Since you are targeting a legacy version of Swift, replace your current implementation of numberOfComponents(in:) with the appropriate version below.

Swift 2.3

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

Swift 3

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

The API changes can be seen here: https://developer.apple.com/library/content/releasenotes/General/iOS10APIDiffs/Swift/UIKit.html

Modified UIPickerViewDataSource

Declaration

From

protocol UIPickerViewDataSource : NSObjectProtocol {
func numberOfComponentsInPickerView(_ pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}

To

protocol UIPickerViewDataSource : NSObjectProtocol {
func numberOfComponents(in pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}

iOS - Error Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'

For Swift 3, use these functions:

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

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

}

Swift/iOS : UIPicker display is not refreshed until tapped after changing the datasource programmatically

Figured it out when adding the asynchronous tag to the question... good old rubber duck debugging.

As said by @rmaddy, all UI updates must be done on the main thread.

In this case, replace

dossiers_picker.reloadAllComponents()

with

DispatchQueue.main.async {
self.dossiers_picker.reloadAllComponents()
}

UIPicker selectRow not working - what's wrong with this code?

It works if I put it in this method:

-(void) viewWillAppear: (BOOL) animated

Swift Picker Items

You should use this:

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

instead of:

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

The diff is in Omitting Argument Labels.
Here is a doc link

Swift 2.1 - UIPickerView doesn't work well

Assuming you have set your delegate and datasource correctly in storyboard, your issue is incorrect method signature. Notice the spelling mistakes in your method signature (pickeView: UIPickerView, numberOfRowsInCompoment component: Int). Missing r in your pickeView and usage of m instead of n in your numberOfRowsInCompoment.

Try with below function and you should be all set...

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->Int{
return 5
}


Related Topics



Leave a reply



Submit