Picker view does not return always the right value

354 views Asked by At

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!

1

There are 1 answers

0
narb On BEST ANSWER

I found the problem. There is a function for selecting the row :)

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