iOS:UITextField selectAll +编辑开始不起作用
我正在尝试在用户点击字段开始编辑时选择UITEXTFIELD中的文本。这个想法是通常,他们将要先删除整个字段,因此这节省了必须仔细点击并选择全部的步骤。
。
- (IBAction)urlEditingDidBegin: (id)sender;
- (IBAction)urlEditingDidBegin:(id)sender
{
NSLog(@"Select Contents");
[sender setText:@"Why isn't this working"];
[sender selectAll:self];
}
当我点击UITEXTFIELD时,字段中的文本会更改为“为什么不起作用”,这证明该方法是触发的,并且发件人是正确的,但是文本未选择。
我在做什么错?
I'm trying to make the text in a UITextField select when the user taps the field to begin editing. The idea is that normally, they will want to erase the entire field first, so this saves the step of having to double tap and select all.
.h
- (IBAction)urlEditingDidBegin: (id)sender;
.m
- (IBAction)urlEditingDidBegin:(id)sender
{
NSLog(@"Select Contents");
[sender setText:@"Why isn't this working"];
[sender selectAll:self];
}
In interface builder I wired my UITextField's "Editing Did Begin" event to the file's owner and connected it to my urlEditingDidBegin method.
When I tap the UITextField, the text in the field changes to "Why isn't this working" which proves the method is firing and that the sender is correct, however the text does not select.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,如果您想为用户提供一种简单的方法来清除文本,请通过在 xib 中将清除按钮设置为“编辑时显示”或调用
[textField setClearButtonMode: UITextFieldViewModeWhileEditing];
以编程方式选择文本听起来很诱人,但我认为您会遇到更多问题,因为它往往会打开编辑菜单。
Generally, if you want to give the user an easy way to clear the text add a clear by setting the clear button to "Appears while editing" in the xib or by calling
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
Selecting the text programmatically sounds tempting, but I think you are going to run into more problems, as it tends to open the editing menu for instance.
实际上文本已被选择,但没有任何视觉反馈。
例如,看看这个答案
Actually text was selected but without any visual feedback.
For example look at this answer
在.h文件中将委托给自我并通过
< uitextfielddelegate>
set delegate to self and pass
<UITextFieldDelegate>
in your .h file