尝试关闭iOS 8中的UIAlertController

3

Crashlytics在我的应用程序中向我发送了这个错误:

Fatal Exception: NSInternalInconsistencyException
Trying to dismiss UIAlertController <UIAlertController: 0x14de40020> with unknown presenter.

UIKit   
-[UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:] + 584

这个问题只会在iOS 8中出现,但是当我尝试复制这个错误时,在iOS 8中我的alertViews能够正常工作,没有发生任何不良情况。为什么会出现这个问题?
我已经了解到iOS 8中UIAlertView被弃用了,现在我们必须使用UIAlertController,但是当我尝试使用UIAlertController时,我无法关闭警告框并且功能也不同。
请帮助我。
提前感谢您的帮助。 编辑: 我想要更改的代码是这个:
UIAlertView * alerta = [[UIAlertView alloc] initWithTitle: AMLocalizedString(@"alertUpdate", @"")
                                                  message:@"\n"
                                                 delegate:self
                                        cancelButtonTitle:nil
                                        otherButtonTitles:nil];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alerta addSubview:spinner];
[spinner startAnimating];
[alerta show];


UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:dis bundle:nil];
MainVC *main = [storyBoard instantiateViewControllerWithIdentifier:@"MainVC"];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: main];
[self presentModalViewController: navControl animated: YES];

[alerta dismissWithClickedButtonIndex:0 animated:YES];
3个回答

3

替换这个

[self presentModalViewController: navControl animated: YES];
[alerta dismissWithClickedButtonIndex:0 animated:YES];

使用这个

[alerta dismissWithClickedButtonIndex:0 animated:YES];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    [self presentModalViewController: navControl animated: YES];
}]; 

这里提供一种异步方式来创建新的控制器,以确保警告视图被关闭。


1
据Xcode提示,presentModalViewController在iOS 6.0中被弃用,不过用我项目内部的另一个方法替换了代码块中的这行后,它能工作了。感谢,我已经给予答案点赞。 :) - Gram
1
我知道这个被弃用了。我只是编辑了源代码行,以使答案更适合问题。 - Tim

1

不确定您是否已解决此问题,但我遇到了类似的问题,这里有一个潜在的解决方案:

实现UIAlertViewDelegate协议并重写alertView:didDismissWithButtonIndex:方法。

任何呈现或解除视图控制器都应在此方法内完成,以确保在导航到其他位置并且“presenter”变为未知之前,AlertView完全被解除。

您也可以在从UIActionSheet按钮单击时进行此操作。


0

UIAlertController 设置 popoverPresentationController

使用以下代码:

UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                                  message: nil
                                                                           preferredStyle: UIAlertControllerStyleActionSheet];
        [alertController addAction: [UIAlertAction actionWithTitle: @"title" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        }]];
CGRect rect = self.view.frame;
 rect.origin.x = self.view.frame.size.width / 20;
 rect.origin.y = self.view.frame.size.height / 20;
[alertController.popoverPresentationController setPermittedArrowDirections:0];
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = rect;
[alertController setModalPresentationStyle:UIModalPresentationPopover];
[self presentViewController: alertController animated: YES completion: nil];

需要添加一个操作吗? - amurcia
这对我不起作用。当警报显示时,我更改了视图控制器并返回到主VC。当我推送您的代码时,警报出现,但我无法推动主VC。 - amurcia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接