PDA

View Full Version : When to release pushedViewController [iPhone]




sujithkrishnan
Jul 4, 2008, 04:30 AM
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



in my customViewController class if user select a row in the myCustomTableView , 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..



admanimal
Jul 4, 2008, 11:07 AM
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.

louden
Jul 13, 2008, 09:15 AM
Pop them off the view controller whenever you save or cancel out of the child form....

[self.navigationController popViewControllerAnimated:YES];

sujithkrishnan
Jul 14, 2008, 12:30 AM
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.

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...

sujithkrishnan
Jul 14, 2008, 12:33 AM
Pop them off the view controller whenever you save or cancel out of the child form....

[self.navigationController popViewControllerAnimated:YES];

Can i conclude that
[self.navigationController popViewControllerAnimated:YES];
will dealloc the pushed viewController fully?

The default-BACK button will do the same job , right?

admanimal
Jul 14, 2008, 12:56 AM
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...

Here is an example from my own code (which itself is adapted from the many Apple examples that follow this pattern)


- (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.

Can i conclude that
[self.navigationController popViewControllerAnimated:YES];
will dealloc the pushed viewController fully?

The default-BACK button will do the same job , right?

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.