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 {
}
list of countries into uipickerview swift 3
You need to return array count in method numberOfRowsInComponent
and in the titleForRow
you need to return array object, show that it will show in picker.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countries.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return countries[row]
}
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
}
How conform to UIPickerViewDelegate and UIPickerViewDataSource outside of a ViewController?
When I attempt to define a separate class called 'PickerDelegate' that conforms to UIPickerViewDelegate & UIPickerViewDataSource, I get this error in Xcode:
Simply make your PickerDelegate a subclass of NSObject.
class PickerDelegate : NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
This is no imposition, and is necessary, because Cocoa is Objective-C and won't even be able to see your delegate and data source methods otherwise.
swift picker view with numbers displaying question marks
Your problem is with the following:
private func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int? {
return numbers[row]
}
This method should not be private and it should have the proper signature. This delegate method needs to return an optional String
, not an optional Int
.
private func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(numbers[row])
}
How to use two UIPickerViews in one view controller?
Based on the information I have in the question, I'd say that you need to set up the data source & delegate methods to handle the ability to distinguish between which picker instance is calling them.
Using the tag property on the picker view is one strategy.
There should be some if/else or switch statements in the methods that have varying logic depending on whether it's the location or the position picker that's being referenced.
Related Topics
Cleanly Handling /Usr/Local/ with Swift Package Manager and Libevent
Extending Dictionary with Key and Value Constraints
Allow Line Editing When Reading Input from The Command Line
Swift Displaying The Time or Date Based on Timestamp
<= Is Not a Prefix Unary Operator
How to Detect Encoding in Data Based on a String
Navigationview Inside a Tabview Swift UI
How to Convert a Pair of Bytes into a Float Using Swift
Arkit: How to Tell If User's Face Is Parallel to Camera
Swift: Handling an Unexpected Nil Value, When Variable Is Not Optional
Remove Cell When Button Pressed Inside Cell Customtableviewcell
Wkwebview Problems in Macos Mojave
How to Constrain Second Nsviewcontroller Minimum Size in Os X App
How Pass Data from Button in Tableviewcell to View Controller