UIPickerView didSelectRow not called - ios

(Swift, Xcode6, iOS8, iPhone)
In my UIPickerView control, didSelectRow is not being called.
Yes, I have set Splash as the delegate (and datasource) for the picker.
Splash extends the UIViewController class
All the other functions work. No errors, the picker spins, has five rows, and displays the row number as defined in the other functions.
When does the didSelectRow method get called? Do I need to implement a Done button of some kind, or will this method fire when the user stops spinning? Do I need to implement a separate notification? tyvm :)
class Splash: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet var countryPicker : UIPickerView = nil
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return 5
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String {
return "\(row)"
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, forComponent component: Int) -> Int {
println("Row: \(row)")
return row
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

You have the wrong signature, pickerView:didSelectRow:forComponent: returns void, not an integer. Try:
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, forComponent component: Int) {
println("Row: \(row)")
}

Swift 4.2
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("selected row = \(row)") }

Related

how to create a list appears from the bottom in Swift

how can I do a list like this ?
can you give me the name of the method ?
is it PickerView ?
I want a filed ruled City Name and when clicked in ..
a list from the bottom appears
see the example here
For this you should use UIPickerView. And set datasource and delegate methods. This is small example.
Some key points of the implementation.
extension ViewController : UIPickerViewDataSource {
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return "\(row)"
}
}
extension ViewController : UIPickerViewDelegate {
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 10
}
}

UIPickerView default row

If I want to have the default row when the view controller starts as [2] or any other row number, how do I carry that out?
var pickerData = ["2x2","4x4","6x6","8x8","12x12"]
override func viewDidLoad() {
super.viewDidLoad()
totalTilesLabel.text = pickerData[8]
display.text = sentData
self.picker.delegate = self
self.picker.dataSource = self
}
This is the UIPicker datasource and delegate.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
Set the picker view's selected row to whatever initial row you want in the viewDidLoad method. Here's Swift 3 code. Adjust as needed for Swift 2.
self.picker.selectRow(8, inComponent: 0, animated: false)

How to add interval value into UIDatePicker in swift

I want to know how to add interval value into UIDateTimePicker in swift
8am-8:30am
8:30am-9am
9am-9:30am
9:30am-10am
10am-10:30am
10:30am-11am
You can try something like this. Add one UIPickerView in your xib or your storyboard viewController after that set the delegate and datasoucre of that UIPickerView with your viewController. Now create one array of string like this.
var timeArr: [String] = [String]()
Now initialize this array in viewDidLoad like this
override func viewDidLoad() {
super.viewDidLoad()
self.timeArr = ["8:00AM - 8:30AM", "8:30AM - 9:00AM", "9:00AM - 9:30AM", "9:30AM - 10:00AM"]
}
Now add the delegate and datasource method of UIPicker in your ViewController like this
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.timeArr.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.timeArr[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(self.timeArr[row])
}
This will create Picker that you want to select time.

Picker view does not return always the right value

I've defined a picker which does not return always the right value. Sometimes it just works but most of the time when I pick the item pickerView returns the wrong value.
I did associate the outlet delegate to the view controller and am using the following code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
var categories = ["animals", "profession", "sea_animals", "food", "tools", "misc", "all"]
var cat: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
println("cat size: \(categories.count)")
return categories.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
println("cat: \(categories[row])")
return categories[row]
}
}
it works sometime but it is not reliable.
Any idea what I do wrongly?
Thanks!
I found the problem.
There is a function for selecting the row :)
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

Delegate Methods in Swift for UIPickerView

Just getting started in Swift and having trouble calling the delegate methods for a UIPickerView
So far I have added the UIPickerViewDelegate to my class like so:
class ExampleClass: UIViewController, UIPickerViewDelegate
I have also created my UIPickerView and set the delegate for it:
#IBOutlet var year: UIPickerView
year.delegate = self
Now I am just having trouble turning the following into Swift code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
Any help would be appreciated
That's actually a method in the UIPickerViewDataSource protocol, so you'll want to make sure you set your picker view's dataSource property as well: year.dataSource = self. The Swift-native way seems to be to implement protocols in class extensions, like this:
class ExampleClass: UIViewController {
// properties and methods, etc.
}
extension ExampleClass: UIPickerViewDataSource {
// two required methods
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return 5
}
}
extension ExampleClass: UIPickerViewDelegate {
// several optional methods:
// func pickerView(pickerView: UIPickerView!, widthForComponent component: Int) -> CGFloat
// func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat
// func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String!
// func pickerView(pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString!
// func pickerView(pickerView: UIPickerView!, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView!
// func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
}
The delegate isn't responsible for calling that method. Instead it is called by the UIPickerView's data source. These are the two functions called by the UIPickerView data source that you need to implement:
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int
To ensure these functions get called, your class should also implement the data source protocol:
class ExampleClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
And your picker's data source should be set:
year.dataSource = self

Resources