PDA

View Full Version : Properly unloading/re-initializing a ViewController




Wunk
Nov 17, 2008, 12:39 PM
I'm using presentModalViewController to pop a new viewcontroller into display which loads certain things like a UIWebView or a remote UIImage. To dismiss these I call
[self dismissModalViewControllerAnimated:YES];

This works just fine, but it doesn't actually unload or free the current viewController. Except for the obvious memory hog this will cause eventually, it's also a pain when loading a UIWebView, the first time it'll properly load the selection you made, but dismissing it and calling that same ModalViewController with a new selection will keep the old UIWebView loaded.

What's the proper way to unload these viewcontrollers so that they get re-initialized upon calling them again ?



Wunk
Nov 17, 2008, 01:06 PM
Well this kind of works.., but there's got to be a better way :-)


- (void)viewDidDisappear:(BOOL)animated {
NSLog(@"Release HTML view");
[super didReceiveMemoryWarning];
}

PhoneyDeveloper
Nov 17, 2008, 02:07 PM
The view controller will be dealloced when it's popped if you've set things up that way. That's how I prefer to do it. Is that not your intent? How are you displaying the view in the first place?

Wunk
Nov 17, 2008, 03:43 PM
The main nib is a UIViewController with UIButtons in an interface tied to an IBAction that calls functions like:


- (IBAction)loadcolumn:(id)sender {
NSLog(@"Column button pressed");
[self presentModalViewController:AddColumnController animated:YES];
}


This will pop up a ViewController tied to another nib in an animation from below to a new new class which holds something like a UIWebView

That class has a function which is tied to a button that says:

- (IBAction)gaTerug:(id)sender {
NSLog(@"Backbutton column pressed");
[self dismissModalViewControllerAnimated:YES];
}


But dismissing it will not unload it, pressing the button again will pop up the previously saved view and in case of a UIWebView it'll not re-initalize the 'viewDidLoad' function with the new NSURL value..

PhoneyDeveloper
Nov 17, 2008, 06:50 PM
So instead of that just build the modal view controller when you need it.

MyViewController* controller = [[MyViewController alloc] init];
[self presentModalViewController: controller animated:YES];
[controller release];

Then when it's popped it will be dealloced.

Do you just load up all the view controllers when your main view controller loads? That will use up a lot of memory.

Wunk
Nov 18, 2008, 11:16 AM
Hm, I'll try that, thanks..

And I thought viewcontrollers don't get loaded until they're actually called, I need to doublecheck that indeed and change the function where needed.


Basically there's a

IBOutlet ColumnViewController *AddColumnController;


In the main viewcontroller's .h file which is tied to a button through Interface Builder, I didn't think it'd preload all these viewcontrollers in advance but instead would only initialize them as soon as the button was pressed..