Swift 如何在另一个 UITextField 突出显示或具有值时禁用 TextField
Swift How to disable a TextField while another UITextField is highlighted or has value
我有三个文本字段:tempTextField、airPressureTextField 和 airDensityTextField。我想在文本字段之间创建一个检查以获得以下逻辑:
如果 tempTextField 或 airPressureTextField 在用户尝试输入值或用户输入值和 returns 处处于焦点位置,我想禁用 airDensityTextField。我试过这段代码。它正确地禁用了 airDensityTextField。但是,如果我从 tempTextField 或 airPressureTextField 中移除焦点并清空这两个文本字段,airDensityTextField 仍处于禁用状态。
func textFieldDidEndEditing(_ textField: UITextField) {
if let t = tempTextField.text, let ap = airPressureTextField.text {
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
我试了textFieldDidEndEditing和textFieldDidBeginEditing中的代码,结果是一样的。
我怎样才能让它发挥作用?
您可以查看 textFieldDidBeginEditing
中正在编辑的文本字段并禁用 airDensityTextField
。然后,当他们完成编辑时,除了是否为 nil 之外,您还需要检查文本是否为 ""(空白)。
class TextFieldVC: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tempTextField: UITextField!
@IBOutlet weak var airPressureTextField: UITextField!
@IBOutlet weak var airDensityTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tempTextField.delegate = self
airPressureTextField.delegate = self
airDensityTextField.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case tempTextField:
airDensityTextField.isEnabled = false
case airPressureTextField:
airDensityTextField.isEnabled = false
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if
let tempText = tempTextField.text,
let apText = airPressureTextField.text,
tempText != "" || apText != "" /// check if at least one of them has text
{
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
}
我有三个文本字段:tempTextField、airPressureTextField 和 airDensityTextField。我想在文本字段之间创建一个检查以获得以下逻辑: 如果 tempTextField 或 airPressureTextField 在用户尝试输入值或用户输入值和 returns 处处于焦点位置,我想禁用 airDensityTextField。我试过这段代码。它正确地禁用了 airDensityTextField。但是,如果我从 tempTextField 或 airPressureTextField 中移除焦点并清空这两个文本字段,airDensityTextField 仍处于禁用状态。
func textFieldDidEndEditing(_ textField: UITextField) {
if let t = tempTextField.text, let ap = airPressureTextField.text {
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
我试了textFieldDidEndEditing和textFieldDidBeginEditing中的代码,结果是一样的。
我怎样才能让它发挥作用?
您可以查看 textFieldDidBeginEditing
中正在编辑的文本字段并禁用 airDensityTextField
。然后,当他们完成编辑时,除了是否为 nil 之外,您还需要检查文本是否为 ""(空白)。
class TextFieldVC: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tempTextField: UITextField!
@IBOutlet weak var airPressureTextField: UITextField!
@IBOutlet weak var airDensityTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tempTextField.delegate = self
airPressureTextField.delegate = self
airDensityTextField.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case tempTextField:
airDensityTextField.isEnabled = false
case airPressureTextField:
airDensityTextField.isEnabled = false
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if
let tempText = tempTextField.text,
let apText = airPressureTextField.text,
tempText != "" || apText != "" /// check if at least one of them has text
{
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
}