OGeek|极客世界-中国程序员成长平台

标题: ios - 显示没有 TextField 的 iOS 屏幕键盘 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 08:52
标题: ios - 显示没有 TextField 的 iOS 屏幕键盘

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

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

所以我让我的 ViewController 实现了 UITextInputTraits 协议(protocol)并向其中添加了这些方法:

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

最后,在 Controller 的 viewDidAppear 方法中,我正在调用 [self becomeFirstResponder]

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

我什至接到了对我的 keyboardAppearance 方法的调用,但无论如何键盘都没有出现(或在它启动时消失)。

我想知道我是否必须实现另一个协议(protocol),例如一种用于接收来自键盘的输入。那会是哪一个?



Best Answer-推荐答案


事实证明,仅实现 UITextInputTraits 是不够的,还需要实现 UITextInput 协议(protocol),以便对键盘上的输入使用react。

在我的 View Controller 的接口(interface)声明中将实现的协议(protocol)更改为UITextInput,并添加这些属性后,键盘出现-仅用于键入需要实现的更多方法:

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

关于ios - 显示没有 TextField 的 iOS 屏幕键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22583029/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4