iOS ARC不取消分配视图 [英] iOS ARC Not deallocating views
问题描述
我们正在创建一个带有主菜单的应用程序,您可以从中导航到带有后退按钮的第二个视图,还有大约6个其他按钮,将6个不同的子视图加载到内存(数组)中,具体取决于您选择的那个视图.
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 wich loads 6 different subviews into memory (array) depending wich one you selected.
当用户选择后退"按钮时,我想从屏幕上用6个按钮分配的内存中删除所有内容.
When the user selects the 'back' button I want to erase anything from memory that was allocated in the screen with the 6 buttons.
此刻,应用程序仅建立了内存,似乎没有任何释放. 请在下面的URL中查看屏幕截图:
At the moment the application just builds up memory amd 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];
}
感谢您的帮助
推荐答案
您创建了一个保留周期.
You created a retain cycle.
您宣布您的代表具有强大的财产.这意味着当你这样做
You declared your delegates as strong properties. It means that when you do
timeView .delegate = self;
timeView
保留self
.
将timeView
作为子视图控制器添加到self
时,self
保留timeView
.
When you add timeView
as child view controller to self
, self
retains timeView
.
如果self
强烈引用tabViews
,则它是tabViews
的所有者,这是添加到其中的对象的所有者,这将导致另一个保留周期:self
拥有tabViews
并拥有<拥有self
的c0>.
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.
这篇关于iOS ARC不取消分配视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!