textFieldShouldBeginEditing 在“Tab"时调用多次按键被按下 [英] textFieldShouldBeginEditing called multiple times when "Tab" key is pressed

查看:123
本文介绍了textFieldShouldBeginEditing 在“Tab"时调用多次按键被按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单屏幕,其中包含多个包含在 UITableView 中的输入字段.如果用户连接蓝牙键盘,则他可以按Tab"键.问题是 textFieldShouldBeginEditing 方法为每个文本字段多次调用.这是正常行为吗?正常行为是,如果某个字段处于焦点并且用户按下 Tab,则光标应跳转到某个其他文本字段,因此 textFieldShouldBeginEditing 将仅被调用一次(对于该文本字段).

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).

看起来这个问题没有解决(post1, post1, post1="https://stackoverflow.com/questions/9584027/textfield-becomefirstresponder-issue-for-tab-keykeyboard-action/20903730#20903730">post2).你们是忽略了这个问题的存在,还是找到了解决方法?

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?

推荐答案

我有一个 UIViewController 在那里我听 UITextFieldDelegate textFieldShouldBeginEditing 并且只有一个特殊的动作我的文本字段.因此,当在蓝牙键盘上按下 Tab 键时,会导致特殊情况触发.

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:

我正在为 Tab 键注册一个 keyCommand,然后让它使用 UIResponder 上的 Category 来查找 firstResponder(当前的 textField),然后通过委托方法触发返回.

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.

您首先需要这个类别来获得 firstResponder:https://stackoverflow.com/a/21330810/747369

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

然后只需注册 keyCommand 并获取当前的 firstResponder.

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];
    }
}

这篇关于textFieldShouldBeginEditing 在“Tab"时调用多次按键被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆