忽略 UITextField 中的制表符...(iPad 应用程序)
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实现此方法:
在 AppDelegate.m 中添加
此方法 在要处理 TAB 键事件的 ViewController.m 或 UITextField 的子类中添加此方法
描述于:如何在 iOS 中设置 Tab 键顺序?
Implement this method:
Add this in your AppDelegate.m
Add this method in the ViewController.m or subclass of UITextField in which you want to handle the TAB key event
Described in: How do you set the tab order in iOS?
检查以确保 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.
我认为这是可能的,但很难。基本上,我会尽力确保当文本字段成为第一响应者时,没有其他视图可以成为第一响应者。然后,按 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.
您是否尝试过检查您调用的 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
这似乎是制表符 (
\t
) 字符的问题。该字符的处理方式与普通字符(例如 a、b、c、0、1、2...)不同,因此永远不会调用委托方法。
在外部键盘或模拟器中使用选项卡的结果是当前活动的文本字段放弃其第一响应者状态,并且结果
将成为第一响应者。
IMO 当前的一个错误(iOS SDK 4.3)是委托方法
仅被调用一次(当您返回 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 thedelegate 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
will become first responder instead.
What IMO currently is a bug (iOS SDK 4.3) is that the delegate method
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.