如何在 UITextField 的 shouldChangeCharactersIn 中有效地处理多个文本字段

How to handle multiple text fields effectively in UITextField's shouldChangeCharactersIn

我有许多文本字段,我想在用户在文本字段中键入时将文本颜色更改为白色。以下是我的代码,其中包含很多似乎效率不高的 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 都将更改