0

I wonder if it's okay to stack 5+ view controllers in an app?

My problem is that I don't know how I can reset/remove old VC's and make a new one the root VC.

My app looks like this

navigation controller -> table view -> menu (modal segue) -> login screen (modal segue) -> account page (push segue) -> table view users images (push segue) -> user image details page (push segue)

The app doesn't crash but there is a lot of VC's stacked when entering the image details page.

Or can I somehow remove table view / menu / login screen from navigation and memory stack when entering account VC? So that account VC becomes root VC.

Gautham Badhrinathan
  • 2,377
  • 3
  • 20
  • 30
Kiwo Tew
  • 1,491
  • 1
  • 22
  • 39
  • You can dismiss view controllers: http://stackoverflow.com/a/24669141/3324388 And the number of view controllers is not so much an issue as is the memory requirements of a view controller. You'll see a memory warning in views (if you have a listener) when that happens. If you get a crash with no console message, that means the memory was eaten up so fast that it crashed and couldnt even print an error. – Aggressor Oct 27 '15 at 19:21

2 Answers2

1

This completely depends on requirement of application flow and memory used by controllers. If you think you are going back to previous pages and process in those classed doesn't pile up memory heap. Then those may remain in stack. Otherwise make sure you keep removing viewControllers from stack.

In your case, I believe you don't really need login/registration page that frequently. So I will suggest change apps rootViewController to homeView after you logged in. And in case of logout change rootView again.

Edit: Here is how to change rootViewController

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UINavigationController *rootViewController = nil;
if (condition) {
    rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];
}else{
    rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
}
_appDelegate.window.rootViewController = rootViewController;
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • Do you have an example of changing which VC should be root? If so I can accept your answer since that would fix my problem. Right now the login screen is shown as a modal segue, when you login in you end up in the homeView. So how do I make the homeView become the root VC? – Kiwo Tew Oct 28 '15 at 09:30
  • @KiwoTew Check Edit in Answer. – rptwsthi Oct 28 '15 at 09:37
  • Had som issues to get it to work since I only use swift but big thanks! Also, is it okey to switch root VC more than one time? – Kiwo Tew Oct 28 '15 at 14:27
0

If you have view controllers on stack, that you never gonna use again, you can do this:

NSArray * old = self.navigationController.viewControllers;
NSArray * importantVCs = @[old[0], [old lastObject]]; //leave only root and top vc
[self.navigationController setViewControllers:importantVCs];

First item in array will be the new root view controller.

Anton Onizhuk
  • 96
  • 1
  • 5