ios 在输入按键时获取 UITextField 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/388237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the Value of a UITextField as keystrokes are entered?
提问by PlagueHammer
Let's say I have the following code:
假设我有以下代码:
IBOutlet UITextField* nameTextField;
IBOutlet UILabel* greetingLabel;
I'd like the greetingLabel
to read "Hello [nameTextField]" as soon as the user presses any key.
我希望greetingLabel
在用户按下任意键后立即读取“Hello [nameTextField]”。
What I need basically is the iPhone equivalent of the Cocoa delegate method controlTextDidChange
.
我需要的基本上是可可委托方法的 iPhone 等价物controlTextDidChange
。
The textField:shouldChangeCharactersInRange:
delegate method is called each time a keystroke occurs:
的textField:shouldChangeCharactersInRange:
委托方法被称为每个键击发生时:
- (BOOL) textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
The string argument returns the character that is pressed. The actual textField
's value (nameTextField.text
) remains blank however.
字符串参数返回按下的字符。但是,实际textField
值 ( nameTextField.text
) 保持空白。
What am I missing here? (I'd like nameTextField
to reflect the exact string that the user has entered so far).
我在这里缺少什么?(我想nameTextField
反映用户迄今为止输入的确切字符串)。
回答by PlagueHammer
It turns out, the easiest way to do this is using Interface Builder:
事实证明,最简单的方法是使用 Interface Builder:
- Add a IBAction (to the ViewController, say, as in this case)
- Ctrl-Click (or right click) on the UITextField in Interface Builder
- Connect the "Editing Changed" event to the File's Owner's IBAction added in the first step.
- 添加一个 IBAction(到 ViewController,比如说,在这种情况下)
- Ctrl-单击(或右键单击)Interface Builder 中的 UITextField
- 将“Editing Changed”事件连接到第一步添加的File's Owner的IBAction。
Works like a charm :) (I can't believe I spent numerous days on this, and to realize now that the solution was much simpler than I'd thought :P)
像魅力一样工作:)(我不敢相信我花了很多天,现在意识到解决方案比我想象的要简单得多:P)
回答by PlagueHammer
you could register an action for the event UIControlEventEditingChanges on the text field:
您可以在文本字段上为事件 UIControlEventEditingChanges 注册一个操作:
[nameTextField addTarget:self action:@selector(updateLabelUsingContentsOfTextField:) forControlEvents:UIControlEventEditingChanged];
...
...
// TODO: error checking
- (void)updateLabelUsingContentsOfTextField:(id)sender {
greetingLabel.text = [NSString stringWithFormat:@"Hello %@", ((UITextField *)sender).text];
}
回答by lostInTransit
UITextField
has a notification UITextFieldTextDidChange
which will be fired every time the text changes if you register for it. Just register for that notification and in the method called by the notification, change the label's text.
UITextField
有一个通知UITextFieldTextDidChange
,如果您注册它,则每次文本更改时都会触发该通知。只需注册该通知,并在通知调用的方法中,更改标签的文本。
To add to this, the object passed to your notification handler will have the text of the UITextField
.
除此之外,传递给通知处理程序的对象将具有UITextField
.
Hope that helps.
希望有帮助。