如何在按下ios上的UINavigationBar后退按钮后隐藏键盘

时间:2012-04-19 12:48:58

标签: ios keyboard back-button

我的键盘显示为textView,我想在用户按下导航栏上的后退按钮时隐藏它。

我试过这个:

-(void)viewWillDisappear:(BOOL)animated{

    [myTextView resignFirstResponder];
}

和此:

-(void)viewDidDisappear:(BOOL)animated{

    [myTextView resignFirstResponder];
}

但它不起作用,我怎么能这样做?

编辑:

我在这里找到了解决方案:

iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet

2 个答案:

答案 0 :(得分:17)

将其放入buttonPress方法 -

[self.view.window endEditing:YES];

编辑 - 这也可以让您在按下“后退”按钮时获取正在编辑的文本的内容

答案 1 :(得分:3)

结合上述答案和检查后退按钮将由此

完成
- (void)viewWillDisappear:(BOOL)animated{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
    // back button was pressed.  We know this is true because self is no longer
    // in the navigation stack.
    [self.view.window endEditing:YES];
}

[super viewWillDisappear:animated];

}