[英]UIPickerView as an inputView of multiple UITextFields
I have three UITextField s in my code and single UIPickerView as in inputView of all text fields. 我的代码中有三个UITextField ,每个文本字段的inputView中都有一个UIPickerView 。
I want to identify, which text field has invoked/opened UIPickerView, inside the UIPickerViewDelegate method pickerView(_:didSelectRow:inComponent:) . 我想在UIPickerViewDelegate方法pickerView(_:didSelectRow:inComponent :)内确定哪个文本字段已调用/打开了UIPickerView 。
Here is sample code I've tried: 这是我尝试过的示例代码:
let tf1: UITextField()
let tf2: UITextField()
let tf3: UITextField()
let pcv: UIPickerView()
tf1.inputView = pcv
tf2.inputView = pcv
tf3.inputView = pcv
// Picker view delegate
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (tf1.isFirstResponder) {
print("tf1")
} else if (tf2.isFirstResponder) {
print("tf2")
} else if (tf3.isFirstResponder) {
print("tf3")
} else {
print("Any other view")
}
}
Is there any other better way to handle this? 还有其他更好的方法来解决这个问题吗?
(Objective C or Swift, any solution) (目标C或Swift,任何解决方案)
on your textfield delegate assign the inputview and tag for your textfield 在您的文本字段委托上为文本字段分配inputview和标签
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.inputView = pcv
pcv.tag = textField.tag
return true;
}
and finally get the tag for pickerview for identify which textfield you tapped. 并最终获得pickerview的标签,以识别您点击了哪个文本字段。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("pickerView == \(pickerView.tag)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.