Mikki Halpin

Total Post:23

Posted by  Mikki Halpin
 1080  View(s)
Rate this:

I have a UITextField and UILabel. When user entered some text to textField and press "Enter" on keyboard I want to save text to the label then clear textField and make it first responder again. But something is going wrong. becomeFirstResponder does not work.


Here is the code of ViewController:

@interface ViewController ()


@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet UILabel *label;




@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    [self.textField addTarget:self action:@selector(saveResult:)




-(void) saveResult: (id) sender {

    self.label.text = self.textField.text;

    self.textField.text = @"";

    [self.textField becomeFirstResponder];




  1. Tarun Kumar


    Re: UITextField becomeFirstResponder does not work in UIControlEventEditingDidEndOnExit

    Here you go, this is a working sample, the "setFrame" methods were just made up out of the blue and you can change it up or just use the delegate methods that do all of this for you, pop this into your code, load up an iphone 6+ simulator, choose to show the hardware keyboard and this will work. There's no reason to add touch event handlers to your UITextField since the delegate methods provided by Apple do all the work for you.

    @interface NSHHomeViewController () <UITextFieldDelegate>



    @property (weak, nonatomic) IBOutlet UITextField *textField;

    @property (weak, nonatomic) IBOutlet UILabel *label;




    @implementation ViewController


        UITextField * _passwordField;

        UILabel * hid;


    - (void)viewDidLoad {

        [super viewDidLoad];


        hid = [UILabel new];

        [hid setFrame:CGRectMake(80, 200, 300, 60)];

        [hid setFont:fontMagicForBoldNavigationBarNSHFont];

        [hid setBackgroundColor:[UIColor babyBlue]];

        [hid setTextColor:[UIColor blackBean]];

        [self.view addSubview:hid];


       _passwordField = [[UITextField alloc] init];

        [_passwordField setSecureTextEntry:TRUE];

        [_passwordField setPlaceholder:@"Password"];

        [_passwordField setReturnKeyType:UIReturnKeyGo];

        [_passwordField setTextAlignment:NSTextAlignmentLeft];

        [_passwordField setDelegate:self];

        [_passwordField setFont:fontMagicForRegularNSHFont];

        [_passwordField setEnablesReturnKeyAutomatically:true];

        [_passwordField setAutocapitalizationType:UITextAutocapitalizationTypeNone];

        [_passwordField setFrame:CGRectMake(30, SCREEN_HEIGHT/2, SCREEN_WIDTH-60,


        UIView *spacerView4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];

        [_passwordField setLeftViewMode:UITextFieldViewModeAlways];

        [_passwordField setLeftView:spacerView4];

        [_passwordField setTextColor:[UIColor NSHBlueColor]];

        [_passwordField setValue:fontMagicForRegularNSHFont forKeyPath:@"_placeholderLabel.font"];

        [_passwordField setClearButtonMode:UITextFieldViewModeAlways];

        [_passwordField setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];

        [self.view addSubview:_passwordField];



    - (BOOL)textFieldShouldReturn:(UITextField *)textField


        if (textField == _passwordField) {

            [hid setText:_passwordField.text];

            [_passwordField setText:@""];

            [_passwordField resignFirstResponder];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)),


    ^{ [_passwordField becomeFirstResponder];




        return TRUE;



    This is just a working example, you need to declare your delegate correctly and declare the delegate of the UITextField correctly as you see above. Then choose which return key type you want for your button on the keyboard, you also then intercept these events by using the UITextFieldDelegate's "texFieldShouldReturn", you don't have to call a timed dispatch queue, but it put this in there for the effect of posting to the label, clearing the text, resigning the keyboard, and then after 1/2 a second, the keyboard becomes first responder again.

      Modified On Aug-17-2015 12:16:31 AM



Enter your email address here always to be updated. We promise not to spam!