My program adds an indefinite amount of views to the screen (determined by the user at runtime), and I keep track of these views by storing them in an array.
Inside my view controller
for (NSString *equationText in equationData) {
// creates a FormulaLabel object for every object in the equationData mutable array
FormulaLabel *tempLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(frame)];
[view addSubview:tempLabel];
[balanceItems addObject:tempLabel]; //balance items keeps track of all the views
When I update my data, I need a different arrangement of views on the screen, so I figured the quickest way to reflect a data change would be to remove all the views currently on the screen and add new views from the updated data.
For this approach, (as far as I know) I need to remove the views from superview, release them, and remove the objects stored in balanceItems. Then I need to init the new views, add them to the subview, and add them to the array. However, the code below generates an error.
for (UIView *view in balanceItems) {
[view removeFromSuperview];
[view release];
}
[balanceItems removeAllObjects];
What is the proper way to remove the views from the superview as well as the array?
First, you should release the views correctly when creating them:
You should release
tempLabelbecause you obtained it with an init method, so you own it.Then when you remove the views; you don’t have to call release:
because the view is currently retained only but the super view and the array, so it will be deallocated when you remove it from them.