How to present UIAlertController when not in a view controller?

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

    There 2 approaches that you can use:

    -Use UIAlertView or 'UIActionSheet' instead (not recommended, cause it deprecated in iOS 8 but it works now)

    -Somehow remember the last view controller which is presented. Here is example.

    @interface UIViewController (TopController)
    + (UIViewController *)topViewController;
    @end
    
    // implementation
    
    #import "UIViewController+TopController.h"
    #import 
    
    static __weak UIViewController *_topViewController = nil;
    
    @implementation UIViewController (TopController)
    
    + (UIViewController *)topViewController {
        UIViewController *vc = _topViewController;
        while (vc.parentViewController) {
            vc = vc.parentViewController;
        }
        return vc;
    }
    
    + (void)load {
        [super load];
        [self swizzleSelector:@selector(viewDidAppear:) withSelector:@selector(myViewDidAppear:)];
        [self swizzleSelector:@selector(viewWillDisappear:) withSelector:@selector(myViewWillDisappear:)];
    }
    
    - (void)myViewDidAppear:(BOOL)animated {
        if (_topViewController == nil) {
            _topViewController = self;
        }
    
        [self myViewDidAppear:animated];
    }
    
    - (void)myViewWillDisappear:(BOOL)animated {
        if (_topViewController == self) {
            _topViewController = nil;
        }
    
        [self myViewWillDisappear:animated];
    }
    
    + (void)swizzleSelector:(SEL)sel1 withSelector:(SEL)sel2
    {
        Class class = [self class];
    
        Method originalMethod = class_getInstanceMethod(class, sel1);
        Method swizzledMethod = class_getInstanceMethod(class, sel2);
    
        BOOL didAddMethod = class_addMethod(class,
                                            sel1,
                                            method_getImplementation(swizzledMethod),
                                            method_getTypeEncoding(swizzledMethod));
    
        if (didAddMethod) {
            class_replaceMethod(class,
                                sel2,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }
    
    @end 
    

    Usage:

    [[UIViewController topViewController] presentViewController:alertController ...];
    

提交回复
热议问题