kmiklas
kmiklas

Reputation: 13433

UIPickerView didSelectRow not called

(Swift, Xcode6, iOS8, iPhone)

In my UIPickerView control, didSelectRow is not being called.

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.
}
}

Upvotes: 0

Views: 3614

Answers (2)

Vishnu Hari
Vishnu Hari

Reputation: 113

Swift 4.2

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

Upvotes: 0

David Berry
David Berry

Reputation: 41226

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)")
}

Upvotes: 1

Related Questions