12

I have a form screen with multiple input fields that are contained inside UITableView. If a user connects bluetooth keyboard then he is able to press "Tab" key. The problem with that is textFieldShouldBeginEditing method is called multiple times for every text field. Is it the normal behaviour? The normal behaviour would be if some field is in focus and the user presses tab then cursor should jump to some other text field and so textFieldShouldBeginEditing would be called only once (for this text field).

It looks like this problem is unsolved (post1, post2). Do you guys ignore the presence of this issue, or have found a fix for that?

Community
  • 1
  • 1
Centurion
  • 14,106
  • 31
  • 105
  • 197
  • Can you please post the output of the following: - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { NSLog(@"Textfield: %@",textField); } – Sebastian Borggrewe Mar 19 '14 at 12:16
  • 1
    @SebastianBorggrewe It will print different textField objects, so it's called for every distinct text field. Already did that. – Centurion Mar 19 '14 at 12:22
  • 1
    check: [link](http://weaklyreferenced.wordpress.com/2012/11/13/responding-to-the-tab-and-shift-tab-keys-on-ios-5-ios-6-with-an-external-keyboard/) for pointers – staticVoidMan Mar 19 '14 at 13:20
  • This is the answer: http://stackoverflow.com/questions/9584027/textfield-becomefirstresponder-issue-for-tab-keykeyboard-action/20903730#20903730 Use textFieldDidBeginEditing – Paul Bruneau Jul 08 '14 at 15:01

1 Answers1

1

I have a UIViewController where I listen to the UITextFieldDelegate textFieldShouldBeginEditing and have a special action on only one of my textfields. So when hitting Tab on a bluetooth keyboard it causes the special case to fire.

Today I've finally found asolution:

I'm registering a keyCommand for the Tab key and then having it use a Category on the UIResponder to find the firstResponder (the current textField) and then firing the return via the delegate method.

You will first need this Category to get the firstResponder: https://stackoverflow.com/a/21330810/747369

Then just register the keyCommand and get the current firstResponder.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self addKeyCommand:[UIKeyCommand keyCommandWithInput:@"\t" modifierFlags:0 action:@selector(tabKeyPressed:)]];
}

- (void)tabKeyPressed:(UIKeyCommand *)sender
{
    id firstResponder = [UIResponder currentFirstResponder];
    if ([firstResponder isKindOfClass:[UITextField class]])
    {
        UITextField *textField = (UITextField *)firstResponder;
        // Call the delegate method or whatever you need
        [self textFieldShouldReturn:textField];
    }
}
Community
  • 1
  • 1
Kramer
  • 519
  • 1
  • 5
  • 13
  • I recently found out that this doesn't work when using Shift+Tab. This would require another KeyCommand with the appropriate modifierFlags. – Kramer Dec 15 '16 at 16:32