我有一個測試項目用於在我的實際項目中實現它之前開發我的代碼。在測試中,我有兩個字段,點擊時應該調出一個填充了該字段數組的數據拾取器。這適用於第一個字段(enterSeason),但第二個(enterSport)只返回鍵盤。我已經通過調試器,變量是正確的,兩個領域的工作都是一樣的。我錯過了什麼?謝謝。數據選取器爲一個字段正確填充,但不是另一個
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet var enterSeason: UITextField!
@IBOutlet var enterSport: UITextField!
var dataPickerView = UIPickerView()
var season = ["2013", "2014", "2015"] //multi-season
//var season = ["2015"] //single-season
var sport = ["Baeball", "Football", "Basketball", "Hockey"]
var activeDataArray = []
override func viewDidLoad() {
super.viewDidLoad()
enterSeason.inputView = dataPickerView
dataPickerView.delegate = self
dataPickerView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidBeginEditing(textField: UITextField) {
if textField == enterSeason {
activeDataArray = season
} else
if textField == enterSport {
activeDataArray = sport
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return activeDataArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return activeDataArray[row] as! String
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if activeDataArray == season {
enterSeason.text = season[row] as! String
}
else if activeDataArray == sport {
enterSport.text = sport[row] as! String
}
}
}
這是我的第一個項目,我從S.O.中學到很多東西。感謝您的耐心答覆。
謝謝。我有一種感覺,它會是我忽略的簡單事情。 – Greg
這些是最好的問題;) – gutenmorgenuhu
我有一個必然的問題。當我從第一個字段(enterSeason)輸入數據,然後點擊第二個字段(enterSport)時,dataPicker仍然具有前一個字段數組的數據。我運行了調試器,它將activeDataArray var設置爲新值,但在選取器中填充的數組不會改變。我是否需要清除選取器?我會怎麼做? – Greg