Keyboard hide and show again right after UIActionSheet dismiss in iOS 7, SDK 7
Asked Answered
E

2

13

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

Evaleen answered 19/11, 2013 at 9:50 Comment(2)
@Mr_bem: I just add some code. It has a TextView with keyboard is ready, a button to open an action sheet, two notifications to get keyboard will show/hide event.Evaleen
Every time a text view becomes first responder it attempts to show the keyboard. If you add the delegate methods for whatever kind of views you use you can track what's going on.Zimmer
W
24

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.

Wilhoit answered 27/12, 2013 at 18:24 Comment(6)
I'm not sure this is future proof. Can you think of a safer solution?Gasometer
What do you by future proof? Why is it unsafe?Wilhoit
By implementing this category you are overriding the UIActionSheet getter, if Apple has or will in the future have any code that is supposed to be executed there - it will be avoided. Therefor it may introduce unexpected behavior in future SDK versions.Gasometer
Probably a much better idea to do it in a UIActionSheet subclass. You don't want to force this behaviour in all the UIActionSheets that may be presented by your app.Acetic
@Acetic Of course, you may. If you need this in many places. But if it is important only for one place of code then you can implement private category.Wilhoit
i added that interfaces into my .m file directly and it work like a charm. :)Tooling
C
0

It is working fine.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if(textField==myTextField2){
        [myTextField1 resignFirstResponder];
        [self showActionSheet];
        return NO;
    }
    return YES;      
}
Chesser answered 13/10, 2014 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.