views:

47

answers:

1

I feel like I'm missing something basic here, and I would appreciate it if you'd help me pull it all together.

Let's say that I have two view controllers... ViewAController and ViewBController.

If I wanted to show viewA, I would do this:

ViewAController *new_view = [[ViewAController alloc] initWithNibName:@"ViewAController" bundle:nil];
self.viewAController = new_view;
[self.view insertSubView:new_view.view atIndex:0];
[new_view release];

If, after showing viewA, I wanted to show viewB controller, I would do this:

[self.viewAController.view removeFromSuperview];
    ViewBController *new_view = [[ViewBController alloc] initWithNibName:@"ViewBController" bundle:nil];
    self.viewBController = new_view;
    [self.view insertSubView:new_view.view atIndex:0];
[new_view release];

Here is my question...

When I load viewA for the first time, viewA's "viewDidLoad" function fires off... It's obviously being loaded for the first time. However, when I remove viewA from the superview, load in viewB, and then later on load viewA again like this:

[self.viewBController.view removeFromSuperview];

if ( self.viewAController == nil ) {

ViewAController *new_view = [[ViewAController alloc] initWithNibName:@"ViewAController" bundle:nil]; self.viewAController = new_view; [self.view insertSubview:new_view.view atIndex:0]; [new_view release];

} else {

[self.view insertSubview:self.viewAController.view atIndex:0];

}

viewA's "viewDidLoad" function does NOT fire off. It's as though viewA has been removed from the view, but it's state is sort of saved in memory. When I load viewA again, it just sort of picks up from where it left off. What I really need it to do is load up as though it's loading for the first time again, with "viewDidLoad", etc...

I hope I have explained this properly. If anyone could provide some enlightenment, I would appreciate it.

+1  A: 

If you don't set self.viewAController to nil, then it won't get away. You probably (hopefully) retained it with your self.viewAController property.

You might release it after loading view B (if you don't need it elsewhere).

If you debug your if/else code, you will probably see that the "if" block is never visited.

Eiko
I had that very same thought at first. I tried this:self.viewAController = nil;But the app crashed. I finally discovered that you need to do this:self.viewAController.view = nil;
tambler