选择器视图并不 return 总是正确的值
Picker view does not return always the right value
我定义了一个选择器,它 return 并不总是正确的值。有时它只是有效,但大多数时候当我选择项目 pickerView returns 时,它的值是错误的。
我确实将出口委托关联到视图控制器并使用了以下代码:
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]
}
}
它有时会起作用,但不可靠。
知道我做错了什么吗?
谢谢!
我发现了问题。
有一个选择行的功能:)
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
我定义了一个选择器,它 return 并不总是正确的值。有时它只是有效,但大多数时候当我选择项目 pickerView returns 时,它的值是错误的。
我确实将出口委托关联到视图控制器并使用了以下代码:
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]
}
}
它有时会起作用,但不可靠。 知道我做错了什么吗? 谢谢!
我发现了问题。 有一个选择行的功能:)
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)