How to make UITextField the selected text

I want to select all the text from a UITextField that was selected when starting editing. I tried the code below, but this does not work.

[txt selectAll:self]; 
+6
source share
3 answers

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

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

And also txt should be a UITextField. Also remember to set the delegate for txt as

  txt.delegate = self; 

where you declared it, and add a UITextFieldDelegate to .h as

  @interface ViewController : UIViewController <UITextFieldDelegate> 

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

+15
source

Xyz answered correctly, but you don't have to use a delegate. You can simply connect the action from the interface designer to β€œEditing really started” and write the same code there.

0
source
 - (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 save its ur string

0
source

Source: https://habr.com/ru/post/914323/


All Articles