Kiwo Tew
Kiwo Tew

Reputation: 1531

Swift iOS is it okay to stack 5 and more view controllers

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.

Upvotes: 0

Views: 247

Answers (2)

rptwsthi
rptwsthi

Reputation: 10182

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;

Upvotes: 1

Anton Onizhuk
Anton Onizhuk

Reputation: 96

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.

Upvotes: 0

Related Questions