soleil
soleil

Reputation: 13135

UINavigationController - detect when navigated back to root view controller

I'm trying to do this by using the delegate method

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

But how can I determine if viewController is the root view controller? It seems there is only a method for topViewController, which is not what I want.

Or is there a better way to determine when I have navigated back to the root of the nav controller by pressing the back button?

Upvotes: 1

Views: 2270

Answers (1)

rmaddy
rmaddy

Reputation: 318954

Check if viewController is the same as the first view controller in the nav stack:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UIViewController *root = navigationController.viewControllers[0];

    if (viewController == root) {
        // moving to the root controller
    }
}

Upvotes: 8

Related Questions