PopViewControllerAnimated:YES 当点击 UIAlertView 时,键盘会出现在 parentVC 中

PopViewControllerAnimated:YES when tap on UIAlertView make Keyboard appear in parentVC

我尝试以编程方式 popViewcontroller 通过这样做

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

   [[self navigationController] popViewControllerAnimated:YES];

}

问题是我在这个 VC.If 中有 textFields,textField 处于活动状态并且键盘正在显示,如果我显示 AlertView 用于命令退出键盘([[self view] endEditing:YES] or [textField resignFirstResponder])。然后调用命令 popViewControllerAnimated:YES 。当前 VC 被关闭,但在父 VC 出现后短暂消失。键盘将显示大约 1 秒,然后消失。

这种行为很烦人。无论如何要解决这个问题?我注意到使用 [[self navigationController] popViewControllerAnimated:NO] 键盘不会出现。但我更喜欢在我的应用程序中使用动画。

请帮忙。

提前致谢

我通过制作 [[self navigationController] popViewControllerAnimated:YES] 解决了这个问题;调用时延迟。

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

@KongHantrakool 的答案有效但也有不足,你可以在 - (void)willPresentAlertView:(UIAlertView *)alertView 中添加 [[self view] endEditing:YES] 或 [textField resignFirstResponder] ,它会更好

您也可以试试这个代码:

#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];
}

试试这个,我认为它可能对你有帮助

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

我也遇到了这个问题,发现延迟解决方案根本不起作用。 alertView 会记住键盘的状态,所以当 alertView 被关闭时,它会恢复键盘。所以问题就出来了:键盘出现在我们弹出 viewController.

后大约 1 秒

这是我的解决方案: 在我们弹出 viewcontroller 之前,我们只需要确保键盘的状态是隐藏的。

  1. 首先我们添加一个属性并注册键盘通知以监控键盘的状态:
[[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. 实施 funs:keyboardDidHide: 和 keyboardDidShow:
- (void)keyboardDidHide:(NSNotification *)notification {
    self.keyboardDidShow = NO;
    if (self.needDoBack) {
        self.needDoBack = NO;
        [self showBackAlertView];

    }
}

- (void)keyboardDidShow:(NSNotification *)notification {
    self.keyboardDidShow = YES;
}
  1. 做你的流行音乐:
- (void)back {
    if (self.keyboardDidShow) {
        self.needDoBack = YES;
        [self.view endEditing:YES];
    } else {
        [self showBackAlertView];
    }
}