我有许多文本字段,我想在用户在文本字段中输入时将文本颜色更改为白色。以下是我的代码,其中包含很多似乎效率不高的 if 条件。有什么办法可以不用写很多 if 条件吗?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == emailTextField {
emailTextField.textColor = .white
} else if textField == nameTextField {
nameTextField.textColor = .white
} else if textField == addressTextField {
addressTextField.textColor = .white
}
return true
}
最佳答案
就这样
textField.textColor = .white
无论 textfield
是什么,它的 textColor
都会被改变
关于ios - 如何在 UITextField 的 shouldChangeCharactersIn 中有效地处理多个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53712543/