Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore Tab character in UITextField... (iPad app)

I have a TableView with TextFields in each cell and I want to those textfields ignore the character tab (\t).

When the tab key is pressed, the textField:shouldChangeCharactersInRange method it's not called

Does anyone knows how to do this? I know that there is no tab key in the iPad keyboard but the blutooth and dock ones do and triggers a really weird behavior.

Thanks

like image 830
Omer Avatar asked Oct 27 '10 18:10

Omer


3 Answers

This seems to be a problem with the tab (\t) character. This character is not handled like normal characters (e.g. a, b, c, 0, 1, 2, ...) and thus the

              - (BOOL)textField:(UITextField *)textField 
  shouldChangeCharactersInRange:(NSRange)range 
              replacementString:(NSString *)string;

delegate method won't ever be called.

The result of using a tab on e.g. an external keyboard or in the simulator is that a currently active textfield resigns it's first responder status and the result of

[textField nextResponder]

will become first responder instead.

What IMO currently is a bug (iOS SDK 4.3) is that the delegate method

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

is only called once (when you return yes) and when you reselect the same textfield and use the tab key again, the method won't be called again.

like image 94
Philipp Frischmuth Avatar answered Nov 19 '22 23:11

Philipp Frischmuth


Implement this method:

Add this in your AppDelegate.m

- (NSArray *)keyCommands {
static NSArray *commands;

static dispatch_once_t once;
dispatch_once(&once, ^{
    UIKeyCommand *const forward = [UIKeyCommand keyCommandWithInput:@"\t" modifierFlags:0 action:@selector(ignore)];
    UIKeyCommand *const backward = [UIKeyCommand keyCommandWithInput:@"\t" modifierFlags:UIKeyModifierShift action:@selector(ignore)];

    commands = @[forward, backward];
});

return commands;
}

Add this method in the ViewController.m or subclass of UITextField in which you want to handle the TAB key event

- (void)ignore {

    NSLog(@"Your Action");
} 

Described in: How do you set the tab order in iOS?

like image 34
Diego Lima Avatar answered Nov 19 '22 23:11

Diego Lima


Check to make sure the delegate for the UITextField is set either in IB or code. Check to make sure your .h file has the UITextFieldDelegate specified

All should work now.

like image 2
Jordan Avatar answered Nov 19 '22 22:11

Jordan