Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
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..
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
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.
 

dgdosen

macrumors 68030
Dec 13, 2003
2,817
1,463
Seattle
Pop them off the view controller whenever you save or cancel out of the child form....

[self.navigationController popViewControllerAnimated:YES];
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
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

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
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

macrumors 68040
Apr 22, 2005
3,531
2
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)

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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.