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

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
huynguyen
  • 7,616
  • 5
  • 35
  • 48
  • @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. – huynguyen Nov 19 '13 at 12:21
  • 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. – David H Nov 19 '13 at 12:25

2 Answers2

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.

malex
  • 9,874
  • 3
  • 56
  • 77
  • I'm not sure this is future proof. Can you think of a safer solution? – Tom Susel Mar 30 '14 at 13:31
  • What do you by future proof? Why is it unsafe? – malex Mar 30 '14 at 14:35
  • 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. – Tom Susel Mar 30 '14 at 18:42
  • 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. – n-b Apr 30 '14 at 08:37
  • @n-b 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. – malex Apr 30 '14 at 09:22
  • i added that interfaces into my .m file directly and it work like a charm. :) – Nhat Dinh May 19 '14 at 05:48
0

It is working fine.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if(textField==myTextField2){
        [myTextField1 resignFirstResponder];
        [self showActionSheet];
        return NO;
    }
    return YES;      
}
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89