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

孤街醉人 提交于 2019-12-10 14:14:07

问题


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!


回答1:


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.




回答2:


My team did a work around by merely unsubscribing from the keyboard notifications before presenting the alert view and resubscribed to those notifications after the alert view had been dismissed. Not ideal but it resolved the issue for us.




回答3:


In my case user clicks the login button, then I call;

[self.view endEditing: YES];
//server request here and in completion/fail alert.

Keyboard was closed, alertview is shown nicely, but on cancel/apply click keyboard was shown again and disappeared.But the problem was this was happening sometimes, if server request takes time problem is not seen, if Alertview is shown immediately problem still there. So I decided to call my alerts with delay. Putting delay on alert is solved my problem. Hope this helps.




回答4:


I just fixed a similar issue. The keyboard keep popup after the alert dismisses It seems like a bug of apple. I recommand you to use UIAlertController instead of UIAlertView. It will avoid a lot of potential issues There is a simple solution: If you are using UIAlertController, you can just set the animated to NO

[self presentViewController:alert animated:NO completion:nil];

Let me know if it fixed your problem



来源:https://stackoverflow.com/questions/30340531/ios-keyboard-notifications-triggered-unnecessarily-upon-showing-the-alertviews-o

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