I’ve wondered today about something.
Let’s say I declare a property MyViewController *myViewController in my root UIViewController interface and implement it (synthesise, release, unload etc.)
I then have a function loadMyView():
- (void)loadMyView {
if (!self.myViewController)
self.myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[self.view addSubview:self.myViewController.view];
}
and:
- (void)hideMyView {
[self.myViewController.view removeFromSuperview];
}
These functions may be used several times.
The thing is, once I hide the view, I no longer need it along with its contents and their current state. So what is the correct way to remove it from memory allowing me to allocate again later (thus omitting the if statement in the loadMyView method) and why?
I need to have a strong pointer to the object for other reasons which is why I’m not creating a temporary object and then releasing it.
In hideMyView do I:
self.myViewController = nil;[self.myViewController release];
or 3. Auto-release memory when initialising: self.myViewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil]autorelease]; (But when does it release?)
Still rather new to this.
What’s confusing about your question is that loadview is one of the methods in the UIViewController class. It doesn’t make sense to override the first view controller’s loadview method in order to initialize a second view controller and add the second view controller’s view property to the the first view controller’s view property. I don’t think your question provides enough context for anyone to give you an intelligent answer.