How to know the UINavigationController is pushing or poping by using UINavigationControllerDelegate?


- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    BOOL isPush = YES;
    UINavigationItem *newTopItem = navigationController.topViewController.navigationItem;
    if ([newTopItem isEqual:navigationController.navigationBar.topItem]) {
        // nothing happened.
        return;
    } else {
        for (UINavigationItem *item in navigationController.navigationBar.items) {
            if ([item isEqual:newTopItem]) {
                isPush = NO;
            }
        }
    }
    NSLog(@"isPush: %d", isPush);
}

NOTE: This code has been tested on iOS 4.2 & iOS 4.3.

WARNING: If you are hiding the UINavigationBar, this code may NOT always get correct result. In that case, [newTopItem isEqual:navigationController.navigationBar.topItem] is always YES.

Why not use UINavigationBarDelegate?

This is a great idea if you do not need to know if the push/pop is animated.

But sometimes, we have to know the animated situation, and we need the navigationController and the viewController instance passed from the delegate.

Comments are closed.