По сути, я просто хочу добавить второй UIPickerView в мой ViewController, но не понимаю, почему следующий код работает правильно.
При выборе второго текстового поля stopTextField Я ожидаю stopPicker UIPickerView появляется, но вместо этого появляется все тот же gradePicker UIPickerView.
Кажется, что компилируется нормально, но полностью игнорирует:
let stopPickerValues = ["What Stop?","Stop 1", "Stop 2", "Stop 3", "Stop 4", "Stop 5", "Stop 6", "Stop 7", "Stop 8", "Stop 9", "Stop 10"]
В чем проблема?
Вот код:
import UIKit
class timeselectviewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var gradeTextField: UITextField!
@IBOutlet weak var stopTextField: UITextField!
var gradePicker: UIPickerView!
var stopPicker: UIPickerView!
let gradePickerValues = ["select approximate arrival time","8:00 am -12:00 pm", "12:00 pm - 4:00 pm", "4:00 pm - 8:00 pm"]
let stopPickerValues = ["What Stop?","Stop 1", "Stop 2", "Stop 3", "Stop 4", "Stop 5", "Stop 6", "Stop 7", "Stop 8", "Stop 9", "Stop 10"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func numberOfComponents2(in pickerView2: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return gradePickerValues.count
}
func pickerView2(_ pickerView2: UIPickerView, numberOfRowsInComponent2 component2: Int) -> Int{
return stopPickerValues.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gradePickerValues[row]
}
func pickerView2(_ pickerView2: UIPickerView, titleForRow2 row2: Int, forComponent2 component2: Int) -> String? {
return stopPickerValues[row2]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
gradeTextField.text = gradePickerValues[row]
self.view.endEditing(true)
}
func pickerView2(_ pickerView2: UIPickerView, didSelectRow2 row2: Int, inComponent2 component2: Int) {
stopTextField.text = stopPickerValues[row2]
self.view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
gradePicker = UIPickerView()
stopPicker = UIPickerView()
gradePicker.dataSource = self
stopPicker.dataSource = self
gradePicker.delegate = self
stopPicker.delegate = self
stopTextField.inputView = stopPicker
gradeTextField.inputView = gradePicker
gradeTextField.text = gradePickerValues[0]
stopTextField.text = stopPickerValues[0]
}
}