tags:

views:

381

answers:

3

Hello all,

I have an UIView added in the main window with a controller. On clik of a button on this view I want to load a UINavigationController which will migrate to multiple views pushing them one by one on stack. Now what I want to do is when user reaches at the end of views, in the last view I have a done button. ON clik of this button I want to move back to my first screen unloading the NavigationController from the memory.

What is the best way to do it since popToRootViewController takes you to the first screen of UINavigationController which is my second screen.

A: 

You basically want to remove the navigation controllers view, so why cant you just say [navigationController.view removeFromSuperView] ?

Daniel
Will it release the memory and all the views cached by the navigationController
rkb
removeFromSUperView wont, but you can release it after that and it will.
Daniel
If the superview is the only thing retaining the navigation controller, then removeFromSuperView will actually release it (and everything it recursively retains).
Tyler
as tyler said, if no other objects retain the navigation controller, it's viewcontrollers, or associated views, than everything will be released. If you are practicing good memory management techniques it should be fine.
Corey Floyd
A: 

One way to do this is to present the navigation controller as a modal view controller, and dismiss it when you're done:

// In the parent controller, when the navigation controller is about to appear:
UINavigationController* navController = [[UINavigationController alloc] init];
[self presentModalViewController:navController animated:YES];

// ... later, in the nav controller, when it's done being used:
[self.parentViewController dismissModalViewControllerAnimated:YES];
[self autorelease];  // goodbye, cruel world (when the ar pool is drained)
Tyler
no Modal view Controller will cover the tabBar at the bottom.
rkb
A modal view covers the tab bar for good reason, to force the user to complete the views and to eliminate confusion. Consider the possibility that you need to rethink your UI instead of fighting the conventions of UIKit (it may be easier).
Corey Floyd
A: 

A few ideas, in order of desirability

  1. make Controller #1 the root view controller of the stack and then use popToRootViewController. Is there a good reason why you aren't doing this already? Keep in mind you can easily hide the navigation bar from any controller, if that's what you're afraid of.

  2. Add a method called "destroyNavigationStack" or something to main Controller #1 and have a reference to controller #1 in your app delegate. In your Nth view controller, when "done" is hit, get a reference to your app delegate (UIApplication's sharedApplication method), and send View Controller #1 this "destroy" message. There really is no reason to even think about popping view controllers off of the stack since you just want to get rid of the entire stack anyway.

  3. Make ViewController #1 a singleton and call destroyNavigationStack

bpapa
Theres no need to make a viewcontroller a singleton...
Daniel
option 1 here is really the best solution for the problem. The first view should be the root view of the navigation controller, and there is no reason I can think of why you wouldn't want to do this.
Corey Floyd
agreed corey. s
Daniel
Daniel, a Singleton would be necessary if he had no reference to View Controller #1 otherwise. I would probably say there are a number of other different ideas that should be listed in between options 2 and 3 and the singleton is one of the worst possible ways to do it, but I believe it would still work.
bpapa