Reputation: 1229
I'm developing an app that resembles the structure of a 'Choose your own adventure' book, with lots of multimedia content (photos and videos mainly). Each 'page' is a ViewController
in which the user has to complete a puzzle or another task in order to go to the next one.
I'm creating a UINavigationController
and I'm pushing every new ViewController
on top of the stack. But I'm worried about having memory issues since there is some heavy multimedia content and I'm not popping any ViewController
, 90% of the time the user can't go back to the previous ViewController
, just forward to a new one.
I'd like an alternative in which every time I jump to a new ViewController
the old one gets released from memory.
Upvotes: 4
Views: 914
Reputation: 93
The UINavigationController class allows you read and write the array of view controllers it uses to implement its stack however you like. You're not restricted to just pushing and popping the view controllers.
In your case, each time a user moves to the next page of the app you should evaluate whether or not you need to keep the last one around. If you do, you can just push it into the stack, however, if you don't, you should pull it out of the array and then use the setViewControllers:animated: method to set the view controller stack appropriately.
Upvotes: 0
Reputation: 10625
If you are not going to go back then you should get rid of your old UIViewController
instances. You can do so after a new instance of UIViewController
has been pushed to the UINavigationController stack.
One option is to just replace the navigationController.viewControllers
array in the viewDidAppear
or viewDidLayoutSubviews
method of your view controllers being pushed to the navigationController
. Means, you are going to need it in every UIViewController
instance.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
navigationController.viewControllers = @[viewController];
}
Second option is useful if you are using a main container controller that handles the pushing of your new UIViewController
instances. Just implement UINavigationControllerDelegate
protocol and implement navigationController:didShowViewController:animated:
method as shown below.
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
navigationController.viewControllers = @[viewController];
}
It should reset your navigationController
s controller stack after a new instance has been pushed.
Obviously you can modify this logic to not remove the ones you want to go back to at some point.
Upvotes: 1