我試圖在同一個viewController上使用兩個不同的pickerView,並讓它們表示不同的數據。我嘗試了不同的方法來做到這一點,但兩個選擇器視圖顯示相同的數據,但不能放在一起。任何幫助都是極好的!swift中同一UIView中的多個自定義UIPickerViews
這裏是我的代碼:
@IBOutlet weak var Eventtype: UITextField!
let pickerView = UIPickerView()
var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]
@IBOutlet weak var datepickertxt: UITextField!
var datepicker = UIDatePicker()
@IBOutlet weak var Duration: UITextField!
let duration = UIPickerView()
var Durations = ["6-8","1-5","7-9","10-12"]
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
pickerView.delegate = self
pickerView.dataSource = self
duration.delegate = self
duration.dataSource = self
Eventtype.inputView = pickerView
Duration.inputView = duration
// Do any additional setup after loading the view.
}
////For Event Picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 0) {
//pickerView1
return Eventstype.count
} else if (duration.tag == 1){
//pickerView2
return Durations.count
}
return 1
//return DurationTxt.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 0) {
return Eventstype[row]
} else if (duration.tag == 1){
return Durations[row]
}
return ""
}
//return DurationTxt[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView.tag == 0) {
Eventtype.text = Eventstype[row]
self.view.endEditing(false)
}else if (duration.tag == 1){
Duration.text = Durations[row]
self.view.endEditing(true)
}
}
您正在檢查標籤,但未爲兩個選取器視圖標籤分配值。 –