如果另一个 UITextField 具有特定值,则启用特定的 UITextField
Enabling a specific UITextField if another UITextField has a certain value
我有两个 UITextField
,cDiseasePicker
和 optionalLMS
。
我的 cDiseasePicker
使用 UIPickerView
作为输入,选项有 "No"、"Yes, 1VD"、"Yes, 2VD" 和 "Yes, 3VD"。
如果用户选择 "No",那么 optionalLMS
应该是灰色的 out/user 交互被禁用。否则,他们应该被允许编辑它。
我试过添加
func textFieldDidEndEditing(_ textField: UITextField) {
if cDiseasePicker.text == "Yes, 1VD" || cDiseasePicker.text == "Yes, 2VD" || cDiseasePicker.text == "Yes, 3VD" {
optionalLMSText.isUserInteractionEnabled = true
print("true cdisease")
} else if cDiseasePicker.text == "No" {
optionalLMSText.isUserInteractionEnabled = false
print("false cdisease")
}
}
但这似乎没有任何效果,控制台也没有打印任何内容。
这是我的 class 声明,如果有不同的话:
class NewRecord: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate
抱歉,如果这是一个基本问题,我对 Swift 还是个新手。
当您使用 UIPickerView
作为输入时,您应该查看选择器视图 didSelectRow
(我猜您正在使用它来填充 cDiseasePicker
文本字段)以进行配置您喜欢的其他文本字段。
如果我理解正确,您需要的关键信息是当文本字段不再是第一响应者时调用 textFieldDidEndEditing
方法,而不是在文本更改时调用。
我有两个 UITextField
,cDiseasePicker
和 optionalLMS
。
我的 cDiseasePicker
使用 UIPickerView
作为输入,选项有 "No"、"Yes, 1VD"、"Yes, 2VD" 和 "Yes, 3VD"。
如果用户选择 "No",那么 optionalLMS
应该是灰色的 out/user 交互被禁用。否则,他们应该被允许编辑它。
我试过添加
func textFieldDidEndEditing(_ textField: UITextField) {
if cDiseasePicker.text == "Yes, 1VD" || cDiseasePicker.text == "Yes, 2VD" || cDiseasePicker.text == "Yes, 3VD" {
optionalLMSText.isUserInteractionEnabled = true
print("true cdisease")
} else if cDiseasePicker.text == "No" {
optionalLMSText.isUserInteractionEnabled = false
print("false cdisease")
}
}
但这似乎没有任何效果,控制台也没有打印任何内容。 这是我的 class 声明,如果有不同的话:
class NewRecord: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate
抱歉,如果这是一个基本问题,我对 Swift 还是个新手。
当您使用 UIPickerView
作为输入时,您应该查看选择器视图 didSelectRow
(我猜您正在使用它来填充 cDiseasePicker
文本字段)以进行配置您喜欢的其他文本字段。
如果我理解正确,您需要的关键信息是当文本字段不再是第一响应者时调用 textFieldDidEndEditing
方法,而不是在文本更改时调用。