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

老子叫甜甜 提交于 2019-11-29 02:39:54

问题


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


回答1:


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.




回答2:


It is working fine.

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


来源:https://stackoverflow.com/questions/20068184/keyboard-hide-and-show-again-right-after-uiactionsheet-dismiss-in-ios-7-sdk-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!