Jak*_*sie 1 memory memory-management objective-c ios
我们正在创建一个带有主菜单的应用程序,您可以使用主菜单导航到带有后退按钮的第二个视图以及大约6个其他按钮,这些按钮将6个不同的子视图加载到内存(数组)中,具体取决于您选择的一个.
当用户选择"后退"按钮时,我想从使用6个按钮在屏幕中分配的内存中删除任何内容.
目前,应用程序只是建立了内存,似乎没有任何东西被释放.请参阅以下网址中的屏幕截图:
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];
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助
您创建了一个保留周期.
您将代理人声明为强大的属性.这意味着当你这样做
timeView .delegate = self;
Run Code Online (Sandbox Code Playgroud)
timeView保留self.
当您添加timeView为子视图控制器时self,self保留timeView.
如果self持有强引用tabViews,那么它是一个所有者tabViews,它是添加到它的对象的所有者,这使得另一个保留周期:self拥有tabViews哪个拥有timeView哪个拥有self.
如果您不想要保留周期,您的孩子对象绝不能强烈提及他们的父母或他们父母的父母.永远不要将代表声明为强大的属性.
至于你的"必须__weak"错误,请看这个答案.