在我的应用程序中,我有一个 UILabel
和 UITextField
。最初 UILabel
文本为零。
一旦用户在 UITextField
中输入一些文本,我的 UILabel
文本也会更新。
假设当用户在 UITextField
my UILabel
中输入 A 时,立即在 UITextField
my UILabel
中显示 A、B显示 B 等等。
为了实现此行为,我使用了 UITextFieldDelegate
的 shouldChangeCharactersInRange
函数。但它总是在一个字符后面。说 UITextField
text = qwerty my UIlabel
text show qwert
请帮助我,以便我可以在用户在 UITextField
中输入值时不断更新 UILabel
文本
这是我的代码
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
mylabel.text = textfield.text
return true
}
最佳答案
您可以为值更改事件注册您的 textField:
[textField addTarget: self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
并在 textFieldDidChange
函数中更新您的标签:
- (void)textFieldDidChange
{
label.text = textField.text;
}
更需要函数 shouldChangeCharactersInRange
来决定是否允许即将发生的变化
关于ios - 当用户在 iOS 的 UITextField 中输入值时,可以不断更新 UILabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30977854/