How to present UIAlertController when not in a view controller?

前端 未结 30 3129
庸人自扰
庸人自扰 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 07:07

    Create Extension like in Aviel Gross answer. Here You have Objective-C extension.

    Here You have header file *.h

    //  UIAlertController+Showable.h
    
    #import 
    
    @interface UIAlertController (Showable)
    
    - (void)show;
    
    - (void)presentAnimated:(BOOL)animated
                 completion:(void (^)(void))completion;
    
    - (void)presentFromController:(UIViewController *)viewController
                         animated:(BOOL)animated
                       completion:(void (^)(void))completion;
    
    @end
    

    And implementation: *.m

    //  UIAlertController+Showable.m
    
    #import "UIAlertController+Showable.h"
    
    @implementation UIAlertController (Showable)
    
    - (void)show
    {
        [self presentAnimated:YES completion:nil];
    }
    
    - (void)presentAnimated:(BOOL)animated
                 completion:(void (^)(void))completion
    {
        UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
        if (rootVC != nil) {
            [self presentFromController:rootVC animated:animated completion:completion];
        }
    }
    
    - (void)presentFromController:(UIViewController *)viewController
                         animated:(BOOL)animated
                       completion:(void (^)(void))completion
    {
    
        if ([viewController isKindOfClass:[UINavigationController class]]) {
            UIViewController *visibleVC = ((UINavigationController *)viewController).visibleViewController;
            [self presentFromController:visibleVC animated:animated completion:completion];
        } else if ([viewController isKindOfClass:[UITabBarController class]]) {
            UIViewController *selectedVC = ((UITabBarController *)viewController).selectedViewController;
            [self presentFromController:selectedVC animated:animated completion:completion];
        } else {
            [viewController presentViewController:self animated:animated completion:completion];
        }
    }
    
    @end
    

    You are using this extension in Your implementation file like this:

    #import "UIAlertController+Showable.h"
    
    UIAlertController* alert = [UIAlertController
        alertControllerWithTitle:@"Title here"
                         message:@"Detail message here"
                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction
        actionWithTitle:@"OK"
                  style:UIAlertActionStyleDefault
                handler:^(UIAlertAction * action) {}];
    [alert addAction:defaultAction];
    
    // Add more actions if needed
    
    [alert show];
    

提交回复
热议问题