3

我知道我必须打电话

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}
//this dismiss keyboard on ios
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView {
    [self.comments resignFirstResponder];
    return YES;
}

为了关闭表单上的键盘,我也知道disablesAutomaticKeyboardDismissal如果我有导航控制器,我必须调用它。

问题是:我以编程方式使用导航控制器创建表单,例如:

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
SettingsViewController *settingsVC = [sb instantiateViewControllerWithIdentifier:@"settingsViewController"];
//add navigation controller 
UINavigationController *modalViewNavController= [[UINavigationController alloc] initWithRootViewController:settingsVC];
modalViewNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modalViewNavController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalViewNavController animated:YES];

所以我没有用于表单的导航控制器类,在这种情况下如何关闭键盘?

我必须创建一个导航视图控制器吗?

更新::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::

最终在故事板中创建导航控制器并调用disablesAutomaticKeyboardDismissal导航控制器的视图控制器,这解决了问题,但我仍然想知道如何在不创建导航控制器类的情况下做到这一点。

欢迎任何意见......

4

2 回答 2

2

你能试试下面的代码吗?

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    gestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:gestureRecognizer];
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}
于 2013-05-21T14:15:29.227 回答
0

下面的代码对我有用:

@interface UINavigationController (dismissKeyBoard)

@end

@implementation UINavigationController (dismissKeyBoard)

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

@end
于 2014-04-21T10:07:43.033 回答