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

标签 ios swift uitextfield

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

相关文章:

iphone - 使用parse实现Hashtag多对多关系?

ios - 如何在 SpriteKit 中制作一个简单的角色 Controller ?

ios - 在 Swift 中为特定方法调用实现闭包

ios - TextField 在编辑时附加到键盘 - 如何使 ScrollView 看起来与编辑前一样?

iOS, swift : Download multiple file serially and showing single progress bar for all file as a one progress

swift - 如何从嵌入式 View 进入标签栏?

swift - 防止 UITextField 中的特殊字符

ios - 用户先前注销后使用无效数据错误地登录

swift - 在 UICollectionView 中点击按钮时字段 UITextField

ios - TextFieldShouldReturn 弄乱了选择器