views:

680

answers:

3

Specifically, what am I supposed to do with the view that is now hidden after pushing on a new view controller?
In my situation I have a view with animations going on, that continue to execute after the view is off screen.

Is there some accepted convention? Do I remove the View Controller and View from memory?
Does Cocoa Touch have a convenient method to "Pause" a view (and controller) and remove it from memory, and bring it back into existence when needed (after a pop)? Do I have to archive it myself and then un-archive it?

Are there any examples you can point me to?

Thanks

+2  A: 

Under low memory conditions, your controller's 'view' property will automatically be set to nil, if it's not on screen. Then the view will automatically load again when it is needed - and viewDidLoad should get called at that time.

If your view controller is retaining any subviews of the top-level view, then you may want to override the view controller's setView: method, check if the view is being set to nil, and if so, release subviews that you were retaining. Otherwise, the top-level view may never get deallocated.

- (void)setView:(UIView *)view
{
    [super setView:view];
    if (view == nil)
    {
        // Release other views
        // self.someView = nil;
    }
}
Chris Lundie
viewDidLoad will be called only once. The view is set to nil only when the view is popped and no longer in the navcontroller's viewcontrollers list.
lostInTransit
You need not override -setView method. You are provided the callback method -viewDidUnload to perform the task which you have mentioned. The view property of viewController is set to nil only in low memory conditions, which if not occurred, the same view is presented to the user after pop operation. I am not sure whether the animations are intelligently handled by the system since the rootViewController's view in no longer in the window view hierarchy when a new viewController is pushed.
Raj
+2  A: 

Another possible solution is to implement two of the following methods:

– viewWillAppear:  
– viewDidAppear:  
– viewWillDisappear:  
– viewDidDisappear:

You could potentially stop your animation in viewWillDisappear or viewDidDisappear and then restart it in viewWillAppear or viewDidAppear. You could also store any necessary state information about the animation before you stop it.

Andy Bourassa
A: 

Whenever you call pushViewController, the current viewcontroller is stored in an array by navigation controller (this can be accessed using the viewControllers property of navcontroller).

Think of it as a stack. As you call pushViewController, a new viewcontroller is added to the stack on top of the current viewcontroller. But your rootviewcontroller is still in memory. When you call popViewController, that viewcontroller is removed from the stack and released.

So if you want to stop your animation when the view disappears, use the viewWillDisappear method as suggested by Andy.

lostInTransit