Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Keyboard notifications triggered unnecessarily upon showing the alertviews on iOS8.3

We are observing unusual behaviour with respect to Keyboard willshow & will hide notification on iOS 8.3.

The viewcontroler (listenig to keyboard notifications) has a textfiled and upon clicking and upon tapping the submit button, the method first resigns the first responder from textfield, and shows an alert to inform warning. Everything works fine, it dismisses the keyboard and shows up the alert as expected. (calls the UIKeyboardWillHideNotification method too).

However, on 8.3, after tapping OK/Cancel on Alertview delegate, it dismisses the alert and it calls up UIKeyboardWillShowNotification & UIKeyboardWillHideNotification respectively, though it was not supposed to be called! This was not expected, as the keyboard was already dismissed before dispalying the alert!

Here is the code snippet, that we are trying:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    }

- (IBAction)ShowAlert:(id)sender {

    [self.TxtField resignFirstResponder];

     //This woudln't make any diff either :(
    [self.view endEditing:YES];

          [self ShowAlertForTest];

}


-(void)ShowAlertForTest{

    UIAlertView *theAlertView= [[UIAlertView alloc]initWithTitle:@"Title"

                                                         message:@"msg"

                                                        delegate:self

                                               cancelButtonTitle:@"Cancel"

                                               otherButtonTitles:@"Yes", nil];

   [theAlertView show];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     NSLog(@"buttonIndex = %ld",buttonIndex);
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSLog(@"keyboardWillShow");
}


- (void)keyboardWillHide:(NSNotification *)aNotification
{
    NSLog(@"keyboardWillHide");
}

This behaviour is causing issues in our app, when there are cascading alerts triggered from the previous alertview'd delegate - bringing up the keyboard in unneeded situations.

Any help /advice is greatly appreciated!

like image 354
My3 Avatar asked May 20 '15 04:05

My3


1 Answers

In our case the keyboard was hidden manually by the app (e.g. when user taps Log In, we hide keyboard and call the server login API). Upon failure the app presents UIAlertView with an error message. When user closes the alert, iOS posts will/did hide & will/did show notifications. Of course the keyboard is not shown & hidden during this sequence, because it is already hidden by the app.

However, we noticed that not hiding keyboard manually, but instead letting iOS to do it for us, fixes the issue. So, the keyboard is hidden automatically in two cases:

  1. when UIAlertView is shown
  2. when view controller is deallocated

Note: the keyboard is automatically shown when UIAlertView is dismissed.

like image 65
Yevhen Dubinin Avatar answered Oct 13 '22 12:10

Yevhen Dubinin