Completion block for popViewController

后端 未结 18 1608
离开以前
离开以前 2020-12-04 06:43

When dismissing a modal view controller using dismissViewController, there is the option to provide a completion block. Is there a similar equivalent for

18条回答
  •  一生所求
    2020-12-04 07:24

    I had the same issue. And because I had to use it in multiple occasions, and within chains of completion blocks, I created this generic solution in an UINavigationController subclass:

    - (void) navigationController:(UINavigationController *) navigationController didShowViewController:(UIViewController *) viewController animated:(BOOL) animated {
        if (_completion) {
            dispatch_async(dispatch_get_main_queue(),
            ^{
                _completion();
                _completion = nil;
             });
        }
    }
    
    - (UIViewController *) popViewControllerAnimated:(BOOL) animated completion:(void (^)()) completion {
        _completion = completion;
        return [super popViewControllerAnimated:animated];
    }
    

    Assuming

    @interface NavigationController : UINavigationController 
    

    and

    @implementation NavigationController {
        void (^_completion)();
    }
    

    and

    - (id) initWithRootViewController:(UIViewController *) rootViewController {
        self = [super initWithRootViewController:rootViewController];
        if (self) {
            self.delegate = self;
        }
        return self;
    }
    

提交回复
热议问题