When to release pushedViewController [iPhone]

Discussion in 'iPhone/iPad Programming' started by sujithkrishnan, Jul 4, 2008.

  1. macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    Hi all.


    In my iPhone app, i am having a viewcontroller with a UITableview, whenever user select a row, i create an instance of same class, push that viewcontroller, but this time the table is loaded with new pair of data

    Code:
    
    in my [I]customViewController [/I]class if user select a row in the [I]myCustomTableView[/I] , i do the following...
    
    // FILE: customViewController.m 
    
    
    if( isNextViewIsTableView)
    {
      customViewController *customViewObj = [[customViewController alloc]init];
      customViewObj.itemArrayToReloadTable = ............   //assign a array
      [[self navigationController]pushViewController: customViewObj animated:YES];
      [customViewObj relelase];
    }
    
    // end of hierarchy view, navigation reached leaf node
    else if( isNextViewIsProductView)  
    {
       productView *productViewObj = [[productView alloc]init];
       productView.textForLabel = ......//assign some string
       prodyuctView.textForNextlabel = ....... //assign some string
       [[self navigationController]pushViewController: productViewObj animated:YES];
      [productViewObj relelase];
    
    }
    
    
    If i am giving like this, if i click back button anytime, its calling the release of the dealloc of that viewController.

    Is this approach is fine?

    I didnt find any such approach in sample codes..
     
  2. macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    If these views are frequently accessed, the recommended technique is to only initialize them once and then reuse the same instance. In that case you would only release it when the parent view is dealloc'ed. Almost all of the Apple sample apps that have more than one View utilize this technique.
     
  3. macrumors 65816

    dgdosen

    Joined:
    Dec 13, 2003
    Location:
    Seattle
    #3
    Pop them off the view controller whenever you save or cancel out of the child form....

    [self.navigationController popViewControllerAnimated:YES];
     
  4. thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #4
    Initailize only once means ???

    Just do the "alloc" for one time and change the values accordingly , right?

    Please see the above code...

    There i am passing the values from present view controller (say the text for label, URL for the image to load etc.. in up-coming view controller)...

    By passing these values from present view controller, the retainCount is changing, So eventhough i call release its not deallocating...
     
  5. thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #5
    Can i conclude that
    [self.navigationController popViewControllerAnimated:YES];
    will dealloc the pushed viewController fully?

    The default-BACK button will do the same job , right?
     
  6. macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    Here is an example from my own code (which itself is adapted from the many Apple examples that follow this pattern)

    Code:
    - (void)showEditBoxForKey:(NSString *)key withObject:(id)editedObject 
    				 withText:(NSString *)textValue withTitle:(NSString *)title numbered:(BOOL)numbered {
    	if(self.editBox == nil) {
    		EditBoxController *eb = [[EditBoxController alloc] init];
    		self.editBox = eb;
    		[eb release];
    	}
    	self.editBox.numbered = numbered;
    	self.editBox.editedObject = editedObject;
    	self.editBox.editedKey = key;
    	self.editBox.textValue = textValue;
    	self.editBox.title = title;
    	[self.navigationController pushViewController:self.editBox animated:YES];
    }
    
    The editBox EditBoxController (which is actually a UIViewController that contains a text box and keyboard) is only allocated and initialized once on demand. Subsequent times that it needs to be displayed, the existing instance is used but with different properties.

    When you push a view controller, it is retained by the parent view. Likewise, when you pop the view controller, it is released by the parent. Therefore, popping the view controller will dealloc it if and only if its retain count is 0 after the pop.
     

Share This Page