简体   繁体   中英

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..

  • I tried it with and without animating, but nothing is gonna change.
  • I tried to say self.navigationController.navigationBar pop back, but it do nothing on my bar.

Thanks for help..

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.

除非您对NavigationController的UIViewControllers数组做一些奇怪的事情,否则该方法应为:

[self.navigationController popToRootViewControllerAnimated:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM