How to present UIAlertController when not in a view controller?

前端 未结 30 3047
庸人自扰
庸人自扰 2020-11-22 06:21

Scenario: The user taps on a button on a view controller. The view controller is the topmost (obviously) in the navigation stack. The tap invokes a utility class method call

30条回答
  •  有刺的猬
    2020-11-22 06:58

    Some of these answers only worked partly for me, combining them in the following class method in AppDelegate was the solution for me. It works on iPad, in UITabBarController views, in UINavigationController, en when presenting modals. Tested on iOS 10 and 13.

    + (UIViewController *)rootViewController {
        UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
        if([rootViewController isKindOfClass:[UINavigationController class]])
            rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
        if([rootViewController isKindOfClass:[UITabBarController class]])
            rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
        if (rootViewController.presentedViewController != nil)
            rootViewController = rootViewController.presentedViewController;
        return rootViewController;
    }
    

    Usage:

    [[AppDelegate rootViewController] presentViewController ...
    

提交回复
热议问题