本文介绍了iPhone如何在打字时获取UITextField的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在单独的 UILabel
上显示对 UITextField
所做的更改。有没有办法在用户键入的每个字符后捕获 UITextField
的全文?目前我正在使用此方法,但它不捕获用户输入的最后一个字符。
I'm trying to show changes made to a UITextField
on a separate UILabel
. Is there a way to capture full text of the UITextField
after each character the user types in? Currently I'm using this method, but it does not capture the last character that the user has entered.
我知道 UITextView
有didChange方法,但我找不到<$ c的方法$ C>的UITextField 。
I know that UITextView
has "didChange" method, but I could not find that method for a UITextField
.
//does not capture the last character
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self updateTextLabelsWithText: textField.text];
return YES;
}
如何在输入每个字符后从UITextField中取出文本?
谢谢!
推荐答案
- 首先将一个
UITextField
和UILabel
添加到storyboard / nib - 现在为
UILabel
分配IBOutlet
(这里我使用了 myLabel ) - 将
UITextFieldDelegate
分配给文件所有者,同时在.h文件中实现相同的委托 -
使用这些代码行:
- First add one
UITextField
andUILabel
to storyboard / nib - Now assign
IBOutlet
forUILabel
(Here I have used myLabel) - Assign
UITextFieldDelegate
to file owner, also implement the same delegate in .h file Use these lines of code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self updateTextLabelsWithText: newString];
return YES;
}
-(void)updateTextLabelsWithText:(NSString *)string
{
[myLabel setText:string];
}
希望这会有所帮助。
这篇关于iPhone如何在打字时获取UITextField的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!