Picker视图不会始终返回正确的值

时间:2015-08-23 20:08:21

标签: ios uipickerviewdelegate

我已经定义了一个不会始终返回正确值的选择器。有时它只是工作,但大多数时候我选择项目pickerView返回错误的值。

我确实将插座代理与视图控制器关联,并使用以下代码:

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

}

它有时会起作用,但它不可靠。 知道我做错了什么吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

我发现了问题。 有一个选择行的功能:)

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