Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why UITextField becomeFirstResponder returns NO?

This is my code:

while (true) {
    if ([identificationField becomeFirstResponder]) {

        NSLog(@"finally!");
        break;
    }
    else{
        if ([identificationField canBecomeFirstResponder]) {
            NSLog(@"can");
        }
        else{
            NSLog(@"cannot");
        }
        NSLog(@"%@", identificationField);
        NSLog(@"dadgum!!");

    }
}

This is what it logs:

   2014-02-05 11:33:54.253 Store Test[22488:70b] can

   2014-02-05 11:33:54.254 Store Test[22488:70b] <UITextField:
   0xb5c89a0; frame = (10 1; 300 43); text = ''; clipsToBounds = YES;
   opaque = NO; autoresize = TM+BM; gestureRecognizers = <NSArray:
   0xb5cc7b0>; layer = <CALayer: 0xb5c8b40>>

   2014-02-05 11:33:54.254 Store Test[22488:70b] dadgum!!

Does anyone know why it might do that? As you can see, the text field can become the first responder, it just won't.

like image 561
user1654889 Avatar asked Feb 05 '14 17:02

user1654889


2 Answers

Is your identificationField in UIView hierarchy? If it hasn't added into window and shown it cannot became first responder.

like image 179
aironik Avatar answered Sep 21 '22 01:09

aironik


A UITextField will also refuse to become first responder if its userInteractionEnabled property is NO as I just discovered. I had to explicitly re-enable user interaction on the text field before it would accept first responder status.

Also worth noting is you can force whatever element currently has first responder status to give it up by calling [UIView endEditing:YES] on any superview that contains the current first responder.

like image 36
devios1 Avatar answered Sep 21 '22 01:09

devios1