4

I am currently developing an app that has a TabBarController and each of the tabs contains a navigation controller. This way on each tab I can show details of the rows selected on a view by pushing the viewcontroller to the navigation controller. Each of the views also have an UINavigationItem above them. In this navigation item I placed a button.

But now I would like to change the viewcontroller for a certain tab, when clicking the button in the UINavigationItem, BUT the view(controller) I want to change to has to act like the root view controller of that tab.

So I do not want to push another view on the navigation controller, but just switch to that view (in the same tab) and have that act as the root view controller.

I cannot find a good way to do this, with actually having the views work correctly. They either do not dealloc when I switch views (which would be nice, because I want to keep memory usage to a minimum).

One way of solving this, might be that I add more tabs to my TabBar Controller and just switch to the right tabs when I click the button, but that would be a last resort.

Not really sure if I described this correctly, but I was wondering what the best way is to do this. My preference is having 3 viewcontrollers and switch between them.

Wim Haanstra
  • 5,918
  • 5
  • 41
  • 57

1 Answers1

7

Hopefully I understand your question correctly: you want to basically 'reset' your navigation controller to have a new root.

You can do this by telling your navigation controller that you want to display a new set of view controllers:

[navigationController setViewControllers:[NSArray arrayWithObject:newViewController] 
                                animated:NO];

This will get rid of all view controllers currently on that navigation controller's stack, and reset the root view to newViewController.

lxt
  • 31,146
  • 5
  • 78
  • 83
  • Hi I believe this works if you have a NavigationController as the self.window.rootViewController. However, I'm implementing a TabBarViewController and self.window.rootViewController = tabBarViewController. Anyway I can still achieve the same results like the original UITabBarController. – Mickey Cheong Sep 26 '12 at 16:15
  • Where we should do that. (ViewWillAppear or tabbarController delegate method) – umakanta Jul 07 '16 at 07:50