1

We are creating an application with a main menu from which you can navigate to a second view with a back button and about 6 other buttons which loads 6 different subviews into memory (array) depending which one you selected.

When the user selects the 'back' button I want to erase anything from memory that was allocated in the screen with the 6 buttons.

At the moment the application just builds up memory and nothing seems to get deallocated. Please see the screenshot in the URL below:

http://oi41.tinypic.com/jfi8ma.jpg

 //Load all tab views into memory before loading them into an array
TimeViewController *timeView = [[TimeViewController alloc]init];
LocationViewController *locationView = [[LocationViewController alloc]init];
DropOffViewController *dropOffView = [[DropOffViewController alloc]init];
CompanyViewController *companyView = [[CompanyViewController alloc]init];
PaymentViewController *paymentView = [[PaymentViewController alloc]init];

//Set delegates of the tab views
timeView .delegate = self;
locationView.delegate = self;

//Load all tab views into array
[tabViews insertObject:timeView atIndex:0];
[tabViews insertObject:locationView atIndex:1];
[tabViews insertObject:dropOffView atIndex:2];
[tabViews insertObject:companyView atIndex:3];
[tabViews insertObject:paymentView atIndex:4];

for(int x = 0; x<5;x++)
{
    UIViewController *tempView = [[UIViewController alloc]init];
    tempView = [tabViews objectAtIndex:x];
    
    [self addChildViewController:tempView];
}
4

2 回答 2

5

You created a retain cycle.

You declared your delegates as strong properties. It means that when you do

timeView .delegate = self;

timeView retains self.

When you add timeView as child view controller to self, self retains timeView.

If self holds strong reference to tabViews, then it's an owner of tabViews, which is an owner of objects added to it, which makes another retain cycle: self owns tabViews which owns timeView which owns self.

If you don't want retain cycles, your child objects must never hold strong references to their parents or any of their parents' parents. Never declare delegates as strong properties.

As for your "must be __weak" error, please see this answer.

于 2013-06-25T07:08:24.387 回答
0

Try with this code to erase your memory on back button action

for(UIView *view in self.view.subviews)
{
    [view removeFromSuperView];
    view=nil;
}   
于 2013-06-25T06:17:30.507 回答