如何找出UITextField导致UIKeyboardWillShowNotification的原因?

Dan*_*n J 15 iphone keyboard nsnotifications uitextfield

我正在尝试在我的应用程序中使用自定义键盘,但在尝试将其限制为一个特定的UITextField时,我遇到了问题.

我的代码基于这个Xcode项目(最初在这个博客上找到).该代码将自定义UIButton(表示"小数点")添加到UIKeyboardTypeNumberPad键盘视图中.它通过订阅UIKeyboardWillShowNotification并在出现时修改键盘来实现.

Xcode项目工作得很好,但是当我添加一个额外的UITextField时,即使我为该文本字段选择了完全不同的键盘类型,自定义键也会被放入该文本字段的键盘中.

我试图注册只看到来自一个特定UITextField的UIKeyboardWillShowNotification通知,但这似乎不起作用:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:exampleViewController.textField];
Run Code Online (Sandbox Code Playgroud)

我还试图检查传递给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};
Run Code Online (Sandbox Code Playgroud)

}}

我误解了addObserver接口吗?当然必须有一种方法来订阅来自特定UI控件的通知吗?

有没有人对如何实现这一点有任何其他建议?

Aja*_*tam 25

无法获得上述任何功能.不得不手动完成:

if ([companyName isFirstResponder]) {
    // ...............
}
else if ([notes isFirstResponder]) {
    //.............
    }
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*lds 0

该项目效果很好,但是当我添加额外的 UITextField 时,自定义键也会被放入该文本字段的键盘中。

您可以为实例设置键盘类型UITextField,例如:

[myTextField setKeyboardType:UIKeyboardTypeDefault];
Run Code Online (Sandbox Code Playgroud)

或者:

myTextField.keyboardType = UIKeyboardTypeDefault;
Run Code Online (Sandbox Code Playgroud)

在 Xcode 帮助中搜索常量UITextInputTraits Protocol列表UIKeyboardType