Chris Chen
Chris Chen

Reputation: 5407

how to know the current view controller is on a navigation stack but not the root one

Currently I'm using the following code to find out if a view controller is in the navigation stack, and it's not the root one in the stack.

self.navigationController.viewControllers.count > 1

Is there any better solutions?

Upvotes: 1

Views: 1578

Answers (2)

AP_
AP_

Reputation: 1033

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count]-2)] animated:YES];

Upvotes: 1

snak
snak

Reputation: 6703

If you convert what you describe directly to code, it'll be something like this.

NSUInteger index = [self.navigationController.viewControllers indexOfObject:self];
BOOL b = index != NSNotFound && index != 0;

The code you wrote may not work when your UINavigationController is not at the top of the stack.

Upvotes: 3

Related Questions