试图解雇UIAlertController iOS 8

时间:2014-10-30 09:23:33

标签: ios objective-c uialertview uialertcontroller

Crashlytics在我的应用中将此错误发送给我:

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

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

此问题仅发生在iOS 8中,但是当我尝试重现此错误时,我的alertViews在iOS 8中正常工作,并且没有任何不妥之处。为什么会出现这个问题?

我已经读过在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 个答案:

答案 0 :(得分: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 :(得分:1)

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

实施 UIAlertViewDelegate协议并覆盖 alertView:didDismissWithButtonIndex:方法。

任何呈现或解除视图控制器都应该这个方法中100%确定在我们导航到别处并且“演示者”变得未知之前,AlertView完全被解除。

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

答案 2 :(得分:0)

popoverPresentationController

设置UIAlertController

使用如下代码:

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