如何找出 UITextField 导致 UIKeyboardWillShowNotification 的原因?
我试图在我的应用程序中使用自定义键盘,但在尝试将其限制为一个特定的 UITextField 时遇到问题。
我的代码基于 这个 Xcode 项目 (最初发现于此博客)。该代码将自定义 UIButton(代表“小数点”)添加到 UIKeyboardTypeNumberPad 键盘视图中。它通过订阅 UIKeyboardWillShowNotification 并在键盘出现时修改键盘来实现。
该 Xcode 项目运行良好,但当我添加额外的 UITextField 时,自定义键也会放入该文本字段的键盘中,即使我为该文本字段选择了完全不同的键盘类型。
我尝试注册以仅查看来自一个特定 UITextField 的 UIKeyboardWillShowNotification 通知,但这似乎不起作用:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:exampleViewController.textField];
我还尝试检查传递给 KeyboardWillShow 的 NSNotification 内的对象,但不幸的是它指的是键盘,而不是 UI 控件这导致键盘出现。
2009-10-21 19:50:22.205 Example[6302:207] NSConcreteNotification 0x321ebd0 {name = UIKeyboardWillShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0.300000011920929;
UIKeyboardBoundsUserInfoKey = NSRect: {{0, 0}, {320, 216}};
UIKeyboardCenterBeginUserInfoKey = NSPoint: {160, 588};
UIKeyboardCenterEndUserInfoKey = NSPoint: {160, 372};
我是否
误解了 addObserver 接口?肯定有一种方法可以订阅来自特定 UI 控件的通知吗?
有人对如何实现这一目标有任何其他建议吗?
I am trying to use a customized keyboard in my application, but I am hitting problems when trying to restrict it to one particular UITextField.
I based my code on this Xcode project (originally found on this blog). That code adds a custom UIButton (representing a 'decimal point') into the UIKeyboardTypeNumberPad keyboard view. It does it by subscribing to UIKeyboardWillShowNotification and modifying the keyboard when it appears.
That Xcode project works great, but when I add an extra UITextField, the custom key gets put into the keyboard for that text field too, even though I have selected a completely different keyboard type for that text field.
I attempted to register to only see UIKeyboardWillShowNotification notifications from the one particular UITextField, but that doesn't seem to work:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:exampleViewController.textField];
I also tried to inspect the object inside the NSNotification passed to keyboardWillShow, but unfortunately it refers to the keyboard, not the UI control that caused the keyboard to appear.
2009-10-21 19:50:22.205 Example[6302:207] NSConcreteNotification 0x321ebd0 {name = UIKeyboardWillShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0.300000011920929;
UIKeyboardBoundsUserInfoKey = NSRect: {{0, 0}, {320, 216}};
UIKeyboardCenterBeginUserInfoKey = NSPoint: {160, 588};
UIKeyboardCenterEndUserInfoKey = NSPoint: {160, 372};
}}
Am I misunderstanding the addObserver interface? Surely there must be a way to subscribe to notifications from a particular UI control?
Has anybody got any other suggestions on how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无法使上述任何一项工作。必须手动完成:
Couldn't get any of the above to work. Had to do it manually:
您可以编写一个 UIView 类别方法来查找第一响应者。
然后在你的
- (void)keyboardWillShow:(NSNotification *)notification
方法中你可以像这样使用它You can write a UIView category method to find the first responder.
Then in your
- (void)keyboardWillShow:(NSNotification *)notification
method you can use it like this该项目效果很好,但是当我添加额外的 UITextField 时,自定义键也会放入该文本字段的键盘中。
您可以为
UITextField
设置键盘类型例如:或:
在
UITextInputTraits Protocol
的 Xcode 帮助中搜索UIKeyboardType
常量列表。That project works great, but when I add an extra UITextField, the custom key gets put into the keyboard for that text field too.
You can set the keyboard type for a
UITextField
instance, e.g.:or:
Search the Xcode help on
UITextInputTraits Protocol
for a list ofUIKeyboardType
constants.@berilcetin 答案的快速版本。
用法
yourView.firstResponder 作为? UITextField
。如果您正在使用
UITableViewCell
A swift version of @berilcetin's answer.
usage
yourView.firstResponder as? UITextField
.if you are working on
UITableViewCell