Show UIAlertController if already showing an Alert

前端 未结 5 924
攒了一身酷
攒了一身酷 2020-12-06 18:15

Difference between the legacy UIAlertView and the new UIAlertController is that the latter needs to be presented onto a specific viewcontroller wit

5条回答
  •  -上瘾入骨i
    2020-12-06 19:02

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

提交回复
热议问题