PopViewControllerAnimated: YES when click on UIAlertView type Keyboard in parentVC

I have tried programmatically popViewcontroller By doing this

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

   [[self navigationController] popViewControllerAnimated:YES];

}

      

The problem is that I have textFields in this VC. If the textField is active and the keyboard is displayed, and if I display the AlertView using the command to cancel the keyboard ( [[self view] endEditing:YES] or [textField resignFirstResponder]

). And then call the command popViewControllerAnimated: YES. The current VC is rejected, but not long after the parent VC appears. The keyboard will be shown, shown as 1 second and then disappear.

This behavior is very annoying. Is there anyway to fix this problem? I noticed that using [[self navigationController] popViewControllerAnimated:NO]

Keyboard does not appear. But I prefer animation in my application.

Please, help.

Thank you in advance

+3


source to share


5 answers


I solved this problem by creating [[self navigationController] popViewControllerAnimated: YES]; call delay.



   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100 *      NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
    [[self navigationController] popViewControllerAnimated:YES];
});

      

+5


source


@KongHantrakool's answer but also has a flaw, you can add [[self view] endEditing: YES] or [textField resignFirstResponder] to - (void) willPresentAlertView: (UIAlertView *) alertView, it will be better.



0


source


You can also try this code:

#pragma mark - UIAlertView Delegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.1];
}

- (void)popViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

      

0


source


Try this, I think it might help you

 - (void)viewWillAppear:(BOOL)animated
 {
      [textField resignFirstResponder];
 }

      

0


source


I ran into this problem too, and found out that the delay solution doesn't work at all. alertView

will remember the status of the keyboard, so when the alertView is fired it will restore the keyboard. So the problem comes out: the keyboard appears about 1 second after we expose the viewController.

Here's my solution: We just need to make sure the keyboard status is hidden before we click on the viewcontroller.

  • First, we add a property and register keyboard notifications to monitor the keyboard state:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
@property (nonatomic) BOOL keyboardDidShow;

      

  1. Implement entertainment: keyboardDidHide: and keyboardDidShow:
- (void)keyboardDidHide:(NSNotification *)notification {
    self.keyboardDidShow = NO;
    if (self.needDoBack) {
        self.needDoBack = NO;
        [self showBackAlertView];

    }
}

- (void)keyboardDidShow:(NSNotification *)notification {
    self.keyboardDidShow = YES;
}

      

  1. Make your pop:
- (void)back {
    if (self.keyboardDidShow) {
        self.needDoBack = YES;
        [self.view endEditing:YES];
    } else {
        [self showBackAlertView];
    }
}

      

0


source







All Articles