4

我有一些 iPad 应用程序,用户将在其中使用触摸屏或蓝牙键盘进行导航。我有一些隐藏的 textView 处于焦点(第一响应者),在这里我检测到从键盘输入的内容。

但是,当我断开键盘时,我遇到了一个问题,出现了虚拟键盘。

我可以检查蓝牙键盘是否已连接,并在 viewDidLoad 或其他内容中设置或退出第一响应者吗?

或者

我有通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];

触发keyboardWillAppear时,我可以以某种方式隐藏键盘吗? 我试过 [textView resignFirstResponder],但没有成功:|

4

3 回答 3

5

您可以将 inputView 设置为透明视图:

UIView *emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
emptyView.backgroundColor = [UIColor clearColor];
textView.inputView = emptyView;

从理论上讲,这会将屏幕键盘设置为空视图,因此它不可见。如果它不接受没有框架的视图,则尝试将宽度和高度设置为 1。它不会影响外部键盘;它只是不会出现在设备上。

于 2012-06-01T00:58:24.030 回答
5

您可以为此使用 performSelector:。

- (void)hideKeyboard:(UITextView *)textView {
    [textView resignFirstResponder];
}

- (void)keyboardWillAppear:(NSNotification *)notification { 
    UITextView *textView = (UITextView *)[self.view viewWithTag:TEXTVIEW_TAG];

    [self performSelector:@selector(hideKeyboard:) withObject:textView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
}
于 2012-05-25T20:32:38.300 回答
0

您必须安排 textView 的第一响应者辞职到调度队列,因为成为第一响应者的过程可能还没有完成。使用 XCode 的调度模板的简单解决方案:

int64_t delayInSeconds = 0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self.textView resignFirstResponder];
});
于 2012-11-20T22:33:00.590 回答