UIActionSheet在iOS 7,SDK 7中closures后,键盘会立即隐藏并显示

我在我的ViewController中创build一个UIActionSheet 。 我还添加了代码来捕获UIKeyboardWillShowNotificationUIKeyboardWillHideNotification通知。

我的问题是当我解雇时,我得到两个通知键盘隐藏并再次显示 。 有人可以告诉我如何防止这个问题? 它只发生在iOS 7中,并用SDK 7构build

更新一些代码:

在viewDidLoad中,我初始化一个button,当触摸button时,会显示操作表。

- (void)viewDidLoad { [super viewDidLoad]; UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(10, 50, 100, 30); [button setTitle:@"Open menu" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)]; [self.view addSubview:textView]; [textView becomeFirstResponder]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{ [searchBar resignFirstResponder]; } - (void) buttonTouched{ UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Action sheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive" otherButtonTitles:@"Hello", nil]; [actionSheet showInView:self.view]; } - (void)keyboardWillShow:(NSNotification*)notification{ NSLog(@"keyboardWillShow"); } - (void)keyboardWillHide:(NSNotification*)notification{ NSLog(@"keyboardWillHide"); } 

我运行的应用程序,键盘将显示,我触摸button,行动表显示。 我通过触摸它的任何button,解雇行动表,并logging打印:

keyboardWillShow

keyboardWillHide

keyboardWillShow

有一个非常简单的解决scheme。 控制器的.m文件中应该添加私有的本地类别

 @interface UIActionSheet (NonFirstResponder) @end @implementation UIActionSheet (NonFirstResponder) - (BOOL)canBecomeFirstResponder { return NO; } @end 

它只有一个副作用。 您的texField / textView在操作表呈现期间保留焦点。 但是我觉得这不是一个很大的麻烦。

也可以用相同的方法来描述UIActionSheet的子类。

它工作正常。

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ if(textField==myTextField2){ [myTextField1 resignFirstResponder]; [self showActionSheet]; return NO; } return YES; }