7

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.

János
  • 32,867
  • 38
  • 193
  • 353
user1654889
  • 319
  • 4
  • 13

5 Answers5

6

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

aironik
  • 105
  • 1
  • 4
  • 2
    This can also occur when the textfield is briefly removed from the view/window hierarchy while becoming the first responder (e.g. reloading a `UITableView` or `UICollectionView` that holds that textfield). – alex-i Feb 18 '16 at 14:29
  • @alex-i what can be done to deal with issue when field is briefly removed while `UICollectionView.reload` – Kashif Apr 13 '23 at 20:13
5

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.

devios1
  • 36,899
  • 45
  • 162
  • 260
2

You probably have another element that is stopping it. Need to know more about your project to really answer with the exact problem.

However, here's a possible you have another textField that has the delegate method

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return NO;
}

That would mean it doesn't resign first responder.

pkamb
  • 33,281
  • 23
  • 160
  • 191
AppHandwerker
  • 1,758
  • 12
  • 22
  • How can I find out what the current first responder is? I click a button to get to the code I posted using an IBAction that is called during "touch up inside," so I don't know what would be the issue unless the UIButton won't resign. – user1654889 Feb 05 '14 at 20:40
  • You'd have to go through all your views and manually ask them if they are firstResponder. You don't want to do that. – AppHandwerker Feb 06 '14 at 13:45
  • Do I have any other options? – user1654889 Feb 09 '14 at 15:34
  • Perhaps if you told us a little more about your view heirachy I could suggest something else. – AppHandwerker Feb 10 '14 at 11:34
  • Thanks, this solved my problem. Worth noting that in my case the textfield with `textFieldShouldEndEditing = NO ` was in *another* view controller. – ChidG Jun 02 '16 at 08:22
1

Whatever is already the first responder can refuse to give up first responder status. In that case, your UITextField won't become the first responder. So you should make sure that whatever is already the first responder isn't refusing to give up that status.

EDIT:

You won't want to leave this in an app when submitted to the app store, but here's some code that uses a private API to determine the current first responder:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

You can use that to see what the first responder is, and that may help you find out why you can't get it to resign, if that's indeed the problem.

Gavin
  • 8,204
  • 3
  • 32
  • 42
  • How can I find out what the current first responder is? I click a button to get to the code I posted using an IBAction that is called during "touch up inside," so I don't know what would be the issue unless the UIButton won't resign. – user1654889 Feb 05 '14 at 20:40
  • I'm having this problem currently, and it's becoming too much of a blocker so I will be deferring it.. But this looked like a really good lead, however firstResponder returned `nil`! And on the very next line of code a textField refused to become first responder. I wish I could step into the code... – dave Sep 19 '14 at 13:13
0

The call to canBecomeFirstResponder only checks if the receiver can itself become the first responder.

The call to becomeFirstResponder will fail and return NO if either the receiver can't become first responder or if the current first responder can't resign.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • How can I find out what the current first responder is? I click a button to get to the code I posted using an IBAction that is called during "touch up inside," so I don't know what would be the issue unless the UIButton won't resign. – user1654889 Feb 05 '14 at 20:39