开发者

iOS: UITextField selectAll + Editing Did Begin not working

开发者 https://www.devze.com 2023-04-05 06:17 出处:网络
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

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 t开发者_C百科he method is firing and that the sender is correct, however the text does not select.

What am I doing wrong?


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


set delegate to self and pass <UITextFieldDelegate> in your .h file

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号