views:

92

answers:

1

So,

I started typically by init the controller from the nib and popping it onto the view stack. But the problem is that the first controller isn't really gone - its still around.

So, we started down the path of this:

Starting w/the appDelegate and loading the RootViewController:

mRootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
[(m42Window *)[application.windows objectAtIndex:0] setController:mRootController];

Going from RootViewController to RegionViewController:

RegionViewController *controller = [[RegionViewController alloc] initWithNibName:@"RegionViewController" bundle:nil];
[[self getWindow] setController:controller];
[controller release];

And the method:

- (void) setController:(m42ViewController *)controller
{   
    if (mController != nil)
    {       
        for (UIView *view in mController.view.subviews)
        {
            if (view != nil) 
            {
                [view removeFromSuperview];
            }
        }       
        [mController.view removeFromSuperview];
        [mController release];
        mController = nil;
    }

    mController = controller;
    [mController retain];
    [self insertSubview:mController.view atIndex:1];
}

Pictures of the issue here: RootViewController: http://mr-sk.com/img/rootViewController.png RegionViewController (Images visible from RootViewController): http://mr-sk.com/img/regionViewController.png

Now the issue is that images in the RootViewController are visible (I have an empty UIImageView that shows images on the controller below it) in the RegionViewController - for whatever reason the view isn't actually being removed from the super view and released. For many reason's we want these views gone:

  1. Memory foot print - why hold onto all kinds of assets we don't need. We can recreate them if the user navigates back
  2. Code - what if code is running in those other controllers. Well, we don't want that in this case. We want them gone.

So, what's wrong? Fundamentally, we've must have missed something in iphone 101 class. heh. I saw we cause this is myself and another guy.

Thanks.

+1  A: 

There are numerous problems with this code:

  • You should really be using a UINavigationController here. Most, if not all, of your issues will go away if you do.
  • It would appear that you're over-retaining the controller passed in to -setController. You're correctly releasing the alloc'd controller in the second code sample (since -setController retains it), but not in the first case.
  • You're removing subviews, and then removing their parent view. There's no reason to do this (and it could cause other problems.)
  • You're not checking to see that mController and the argument passed into -setController are, in fact, different arguments. If they are, you could crash.
  • It would appear that you're using an array of windows? Why?
  • It's unclear what object -setControl: is a member of. A window? A view?
  • Once you fix all this stuff, your problem MAY go away (though, of course, it may be somewhere other than the code you've put here).
Ben Gottlieb
Ok - Say I use a UINavigationController, which I'm used to using in this context: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html However, I dont want anything like that - no toolbars, no tableviews. Basically, I have a stack of menu systems prior to the player getting to the actual OpenGL game view (which they stay on for the majority of the time) which they have to navigate through.I guess I'm used to thinking of UINavigationControllers in the typical UITableView usage patterns...
Mr-sk
You can hide the UINavigationBar in a UINavigationController, and there's no default toolbar. It's used for shuffling views around on screen; the last one in your chain could be your OpenGL game view.
Ben Gottlieb
Ok, cool - I guess I gotta dig around and see how I can best fix my code. I've had pains trying to convert, say a UIViewController to a UINavigationController.
Mr-sk
You don't need to convert it; your existing UIViewController should get added to a UINavigationController. You probably won't need to subclass UINavigationController; it's pretty functional as-is.
Ben Gottlieb