I create an UIActionSheet in my ViewController. I also add code to catch UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notification.
My problem is when I dismiss, I get two notification keyboard hide and show again. Somebody can show me how to prevent that problem ? It only happen in iOS 7 and build with SDK 7
Update some code:
In viewDidLoad, I init a button, when touch button, action sheet will be showed.
- (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");
}
I run app, keyboard will showed, I touch button, action sheet showed. I dismiss action sheet by touch any button on it, and Log print :
keyboardWillShow
keyboardWillHide
keyboardWillShow
There is a very simple solution. One should add private local category in .m file of the controller
@interface UIActionSheet (NonFirstResponder)
@end
@implementation UIActionSheet (NonFirstResponder)
- (BOOL)canBecomeFirstResponder
{
return NO;
}
@end
There is the only one side effect of it. Your texField/textView retains focus during action sheet presenting. But it is not a big trouble I think.
Also one can subclass UIActionSheet in the same way.