Home > DeveloperSection > Forums > UITextField becomeFirstResponder does not work in UIControlEventEditingDidEndOnExit
Mikki Halpin
Mikki Halpin

Total Post:23

Points:161
Posted on    August-13-2015 11:57 PM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 670  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;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

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

        forControlEvents:UIControlEventEditingDidEndOnExit];

}

 

-(void) saveResult: (id) sender {

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

    self.textField.text = @"";

    [self.textField becomeFirstResponder];

}

 

@end



Tarun Kumar

Total Post:213

Points:1635
Posted on    August-17-2015 12:14 AM

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>

@end

 

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

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

 

@end

 

@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,

55)];

    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)),

dispatch_get_main_queue(),

^{ [_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

Don't want to miss updates? Please click the below button!

Follow MindStick