基本上,我只是想向我的ViewController添加第二个UIPickerView,但不明白为什么以下代码可以正常工作。
选择第二个文本字段 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]
}
}
答案 0 :(得分:0)
您可以这样做:
var pickerView: UIPickerView!
...
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if gradeTextField.isFirstResponder {
return gradePickerValues.count
} else {
return stopPickerValues.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if gradeTextField.isFirstResponder {
return gradePickerValues[row]
} else {
return stopPickerValues[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if gradeTextField.isFirstResponder {
gradeTextField.text = gradePickerValues[row]
} else {
stopTextField.text = stopPickerValues[row]
}
self.view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView = UIPickerView()
pickerView.dataSource = self
pickerView.delegate = self
stopTextField.inputView = pickerView
gradeTextField.inputView = pickerView
gradeTextField.text = gradePickerValues[0]
stopTextField.text = stopPickerValues[0]
}
问题是您的pickerView2
方法只是被忽略了。这样,我在上面发布的内容会检查哪个文本字段是执行所需操作或返回正确值的第一响应者。
编辑
正如rmaddy指出的那样,您一次只需要一个选择器视图,因为一次只能有一个文本字段作为第一响应者。
答案 1 :(得分:0)
我同意@ajeferson的回答,但是如果您仍然想同时使用两个选择器视图,请尝试以下操作:
Subject::create([
'label' => 'foo',
'user_id' => auth()->id(),
'parent_id' => 3
])
委托和数据源方法都提供了UIPickerView的每个对象,因此我们可以根据需要使用它们。