我正在尝试学习如何以编程方式将数据填充到UIPickerView中,而不是将选择器作为主故事板上的元素。我正在一个项目中练习,所以当我完成这项工作后,我可以将代码复制到我实际尝试开发的项目中。到目前为止,当我点击季节字段时,屏幕底部会出现灰色选择器,但其中没有数据。我有一种感觉,这是因为我试图填充数据的方式,我从一个关于将数据填充到选择器视图的教程中获得,但是教程在主故事板上有一个选择器元素。我可以就此得到一些指示吗?谢谢。
class ViewController: UIViewController, UIPickerViewDelegate {
@IBOutlet var enterSeason: UITextField!
var dataPickerView = UIPickerView()
var season = ["2013", "2014", "2015"] //multi-season
//var season = ["2015"] //single-season
override func viewDidLoad() {
super.viewDidLoad()
enterSeason.inputView = dataPickerView
//enterSeason.inputView = UIPickerView()
}
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 {
return season.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return season[row] as String
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var itemSelected = season[row] as String
self.enterSeason.text = itemSelected
}
}