Properly unloading/re-initializing a ViewController

Discussion in 'iPhone/iPad Programming' started by Wunk, Nov 17, 2008.

  1. macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #1
    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
    Code:
    [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 ?
     
  2. thread starter macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #2
    Well this kind of works.., but there's got to be a better way :)

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

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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?
     
  4. thread starter macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #4
    The main nib is a UIViewController with UIButtons in an interface tied to an IBAction that calls functions like:

    Code:
    - (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:
    Code:
    - (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..
     
  5. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    So instead of that just build the modal view controller when you need it.

    Code:
    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.
     
  6. thread starter macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #6
    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
    Code:
    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..
     

Share This Page