PopToRootViewController by 3rd level, no pop at the barbutton items

I'm pushing to a few viewcontrollers with my UINavigationController and when I'm on my 3rd UISubViewController I coded to 'popToRootViewController' and it pop's back, but the navigationbar-items push to the 2nd view controller not to the 1st..

Thanks for help..

Upvotes: 1

Views: 1380

Answers (2)

Matt
Matt

Reputation: 1265

I am a little confused at your question but I think this should help. As far as I know you can't have the "back" button on a UINavigationController go back more than 1 pop. So what you do is you have to add another button that does the popToRootViewController function. Try something like this:

UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStylePlain target:self action:@selector(homeAction:)];

self.navigationItem.rightBarButtonItem = newButton;

This will add a button to the right on your navigation controller. The next step is to add a function called whatever you name in the action (in this case it needs to be called "homeAction"). This looks like so:

-(IBAction)homeAction:(id)sender {
      [self.navigationController popToRootViewControllerAnimated:YES];
}

This should cover the pop back to the root of the controller where ever you are in the stack. You will also have to IBAction method to your .h file but other than that it should do everything you want to.

Upvotes: 1

Bittu
Bittu

Reputation: 676

unless you are doing something odd with the array of UIViewControllers of NavigationController, the method should be:

[self.navigationController popToRootViewControllerAnimated:YES];

Upvotes: 0

Related Questions