首页 > 解决方案 > 如果另一个 UITextField 具有特定值,则启用特定 UITextField

问题描述

我有两个UITextFieldscDiseasePickeroptionalLMS.

cDiseasePicker使用 aUIPickerView作为输入,选项有“No”、“Yes, 1VD”、“Yes, 2VD”和“Yes, 3VD”。

如果用户选择“否”,optionalLMS则应显示为灰色/禁用用户交互。否则,他们应该被允许编辑它。

我试过添加

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 NewRecord: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate

抱歉,如果这是一个基本问题,我对 Swift 还是很陌生。

标签: iosswiftuitextfield

解决方案


当您使用 aUIPickerView作为输入时,您应该查看您的选择器视图didSelectRow(我猜您正在使用它来填充您的cDiseasePicker文本字段)以根据自己的喜好配置其他文本字段。

如果我理解正确,您需要的关键信息textFieldDidEndEditing是在文本字段不再是第一响应者时调用该方法,而不是在文本更改时调用。


推荐阅读