UINavigationController在pushViewController之后做一些操作

经常遇到pushViewController之后需要做一些操作的情况,官方文档没提供接口这么处理,理论上是可以的,那具体的操作方式就是用CATransition把pushViewController包起来,然后在设置一个complete block。

[CATransaction begin];
[[self navigationController] pushViewController:viewController animated:YES];
[CATransaction setCompletionBlock:^{
    //do something
}];
[CATransaction commit];

当然也可以使用category添加一个方法来做这个事情:

@interface UINavigationController (complete)

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completedBlock:(dispatch_block_t)block;

@end

@implementation UINavigationController (complete)

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completedBlock:(dispatch_block_t)block {
    [CATransaction begin];
    [self pushViewController:viewController animated:animated];
    [CATransaction setCompletionBlock:^{
        if (block) {
            block();
        }
    }];
    [CATransaction commit];
}

@end