当textview成为焦点时,SDK安全隐藏iPad键盘的方法

时间:2012-05-25 20:08:41

标签: objective-c ios ipad uiview

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

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

我可以检查蓝牙键盘是否已连接,是否可以在viewDidLoad中设置或取消第一响应者?

我有通知:

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

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

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:5)

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

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

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

答案 2 :(得分: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];
});