当前位置: 动力学知识库 > 问答 > 编程问答 >

ios8 - Show UIAlertController if already showing an Alert

问题描述:

Difference between the legacy UIAlertView and the new UIAlertController is that the latter needs to be presented onto a specific viewcontroller with presentViewController:animated:completion:. This poses an awkward problem for my use case: what if there is already an UIAlertController showing (e.g. a rating dialog) when a second viewcontroller gets presented (e.g. an error dialog due to failed network connection). I have experienced that in this case the second UIAlertController just does not show.

Edit: At the moment I try to show an alert, I do not know if there currently is anything presenting.

How do you cope with this situation?

网友答案:

I found a workaround to find out which viewcontroller I can present the alert upon. I also posted the answer here:

@implementation UIViewController (visibleViewController)

- (UIViewController *)my_visibleViewController {

    if ([self isKindOfClass:[UINavigationController class]]) {
        // do not use method visibleViewController as the presentedViewController could beingDismissed
        return [[(UINavigationController *)self topViewController] my_visibleViewController];
    }

    if ([self isKindOfClass:[UITabBarController class]]) {
        return [[(UITabBarController *)self selectedViewController] my_visibleViewController];
    }

    if (self.presentedViewController == nil || self.presentedViewController.isBeingDismissed) {
        return self;
    }

    return [self.presentedViewController my_visibleViewController];
}

@end

// To show a UIAlertController, present on the following viewcontroller:
UIViewController *visibleViewController = [[UIApplication sharedApplication].delegate.window.rootViewController my_visibleViewController];
网友答案:

Since UIAlertControllor is itself a UIViewController, you can present a second UIAlertController on top of the first one by presenting from the existing one:

                alertController.PresentViewController(alertController2,  animated: true, completionHandler: null);
网友答案:

This code fulfilling the requirement when app has to present some alert on window and before present its checking that if there is any other AlertController presented already, if presented then present the alert on appeared Alertcontroller otherwise present it on window.

Here is one more alternative, you can optimize it according to your requirement.

     func showAlert(message:String) {

        if let alert = self.checkIfAlertViewHasPresented() {
            alert.presentViewController(alertController, animated: true, completion: nil)

        } else {
            self.window?.rootViewController!.presentViewController(alertController, animated: true, completion: nil)
        }
    }

    func checkIfAlertViewHasPresented() -> UIAlertController? {

        if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
            while let presentedViewController = topController.presentedViewController {
                topController = presentedViewController
            }
            if topController is UIAlertController {
               return (topController as! UIAlertController)
            } else {
               return nil
            }
        }
        return nil
    }
网友答案:

This is what I'm using. this way if alert is already display, I prefer the user to dismissed it and not the app. So, incase the view is already presenting alert, I just wait 5 seconds and try again.

I just want to add, I did not test this too much, but it works.(from 1 test I made), so I hope I'm not missing something, cause I thought about this problem for long time, and this solution sound too easy :)

-(void) alertUserWithTitle:(NSString*) title Message:(NSString*) message
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:title                                                                              message:message
                                                                            preferredStyle:UIAlertControllerStyleAlert];

                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                          handler:^(UIAlertAction * action) {}];

                    [alert addAction:defaultAction];

                    if(self.presentedViewController == nil)
                    {
                        [self presentViewController:alert animated:YES completion:nil];
                    }else
                    {
                        double delayInSeconds = 2.0;
                        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

                        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

                            [self alertUserWithTitle:title Message:message];

                        });

                    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: