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:
- Memory foot print - why hold onto all kinds of assets we don't need. We can recreate them if the user navigates back
- 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.
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).
精彩评论