忽略 UITextField 中的制表符...(iPad 应用程序)

发布于 2024-09-29 14:24:10 字数 228 浏览 4 评论 0原文

我有一个 TableView,每个单元格中都有 TextFields,我想要这些文本字段 忽略字符制表符 (\t)。

当按下 Tab 键时,不会调用 textField:shouldChangeCharactersInRange 方法

有人知道如何执行此操作吗?我知道iPad上没有tab键 键盘,但蓝牙和坞站会触发一种非常奇怪的行为。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

罪歌 2024-10-06 14:24:11

实现此方法:

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

此方法 在要处理 TAB 键事件的 ViewController.m 或 UITextField 的子类中添加此方法

- (void)ignore {

    NSLog(@"Your Action");
} 

描述于:如何在 iOS 中设置 Tab 键顺序?

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?

薄情伤 2024-10-06 14:24:11

检查以确保 UITextField 的委托是在 IB 或代码中设置的。
检查并确保您的 .h 文件已指定 UITextFieldDelegate

现在全部应该可以工作。

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.

昔日梦未散 2024-10-06 14:24:11

我认为这是可能的,但很难。基本上,我会尽力确保当文本字段成为第一响应者时,没有其他视图可以成为第一响应者。然后,按 Tab 键将不会执行任何操作。然后,当选择另一个实际上可能成为第一响应者的视图时,或者当文本字段放弃第一响应者时,您将必须反转此效果。

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.

青柠芒果 2024-10-06 14:24:11

您是否尝试过检查您调用的 shouldChangeCharactersInRange 范围内的其他字符?这将确保它没有被正确调用(特别是针对 Tab 键的问题)。

有关 shouldChangeCharactersInRange 的更多信息此处

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

北笙凉宸 2024-10-06 14:24:10

这似乎是制表符 (\t) 字符的问题。该字符的处理方式与普通字符(例如 a、b、c、0、1、2...)不同,因此

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

永远不会调用委托方法。

在外部键盘或模拟器中使用选项卡的结果是当前活动的文本字段放弃其第一响应者状态,并且结果

[textField nextResponder]

将成为第一响应者。

IMO 当前的一个错误(iOS SDK 4.3)是委托方法

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

仅被调用一次(当您返回 yes 时),当您重新选择相同的文本字段并再次使用 Tab 键时,该方法将不会再次被调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文