在没有TextField的情况下显示iOS屏幕键盘

时间:2014-03-22 20:16:49

标签: ios iphone keyboard

我有一个特殊的iPad应用程序,即使没有UITextField作为第一响应者,屏幕键盘也必须保持可见。

据说,出现的键盘由UITextInputTraits控制。

所以我让我的ViewController实现了UITextInputTraits协议并将这些方法添加到它:

- (UITextAutocapitalizationType) autocapitalizationType { return UITextAutocapitalizationTypeNone; }
- (UITextAutocorrectionType) autocorrectionType { return UITextAutocorrectionTypeNo; }
- (UITextSpellCheckingType) spellCheckingType { return UITextSpellCheckingTypeNo; }
- (UIKeyboardAppearance) keyboardAppearance { return UIKeyboardAppearanceDefault; }
- (UIKeyboardType) keyboardType { return UIKeyboardTypeAlphabet; }
- (UIReturnKeyType) returnKeyType { return UIReturnKeyDefault; }
- (BOOL) enablesReturnKeyAutomatically { return NO; }
- (BOOL) secureTextEntry { return NO; }

最后,在控制器的viewDidAppear方法中,我调用了[self becomeFirstResponder]

但这显然不足以打开键盘。我错过了什么?

我甚至打电话给我的keyboardAppearance方法,但无论如何键盘都没有出现(或在键盘启动时消失)。

我想知道是否必须实施其他协议,例如一个用于接收键盘输入。会是哪一个?

2 个答案:

答案 0 :(得分:1)

事实证明,实现UITextInputTraits是不够的,但需要实现UITextInput协议,以便人们可以对键盘上的输入作出反应。

在我的视图控制器的接口声明中将实现的协议更改为UITextInput并添加这些属性后,键盘出现 - 仅用于键入更多要实现的方法:

@property (nonatomic, readonly) UITextRange *markedTextRange;
@property (readwrite, copy) UITextRange *selectedTextRange;

答案 1 :(得分:1)

使用becomeFirstResponder方法从视图控制器显示键盘的更快速解决方案是实现UIKeyInput协议,并在视图控制器中为YES返回-(BOOL)canBecomeFirstResponder。在这种情况下,将使用UIKeyInput协议方法来处理键盘操作。

另外,只有在需要避免奇怪的行为时,才要小心返回YES canBecomeFirstResponder