Ignore Tab character in UITextField… (iPad app)

坚强是说给别人听的谎言 提交于 2019-12-01 14:05:33

问题


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


回答1:


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.




回答2:


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?




回答3:


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.




回答4:


I think this is possible, but difficult. Basically, I would try to ensure that when the text field becomes the first responder, no other view can become the first responder. Then, pressing tab will do nothing. Then, you would have to reverse this effect when another view that actually could become first responder is selected, or when the text field resigns first responder.




回答5:


Have you tried checking other characters in the range you're calling shouldChangeCharactersInRange with? That will make sure it's not being called properly (vis a vis a problem with the tab key specifically).

more on shouldChangeCharactersInRange here



来源:https://stackoverflow.com/questions/4036510/ignore-tab-character-in-uitextfield-ipad-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!