6

I want to select all text from the UITextField selected when i start editing. I tried the below code but this doesn't work.

[txt selectAll:self];
1

3 Answers 3

15

Please check where you have placed it... Try putting the code above in

       -(void)textFieldDidBeginEditing:(UITextField *)textField
        {
           [textField selectAll:self];

         }

And also txt must be UITextField. Also do not forget to set the delegate for txt as

        txt.delegate = self;

where you have declared it and add UITextFieldDelegate in .h as

       @interface ViewController : UIViewController <UITextFieldDelegate>

This will definitely work....It worked for me..

Sign up to request clarification or add additional context in comments.

Comments

0

Xyz answered correctly, but you don't necessarily need to use delegate. You may just connect action from interface builder on "Editing did begin" and write same code there.

1 Comment

with connection it was not working for me, but with delegate it did.
0
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
          NSString * str = [textField.text stringByReplacingCharactersInRange:range withString:string];
          return YES;}

when you start editing use this code to store it ur string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.