PopViewControllerAnimated:是,当点击UIAlertView使键盘出现在parentVC中



我试图以编程方式弹出Viewcontroller通过这样做

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   [[self navigationController] popViewControllerAnimated:YES];
}

问题是我在这个VC中有文本字段。如果文本字段处于活动状态并且键盘正在显示,并且如果我显示警报视图,并使用用于命令来退出键盘([[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] endEdit: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 关闭时,它将恢复键盘。所以问题出现了:在我们弹出视图控制器大约 1 秒后,键盘出现。

这是我的解决方案:我们只需要在弹出视图控制器之前确保键盘的状态是隐藏的。

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

最新更新