【发布时间】:2023-03-13 04:17:01
【问题描述】:
我们正在创建一个带有主菜单的应用程序,您可以从中导航到带有后退按钮的第二个视图和大约 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];
}
【问题讨论】:
-
您是否将您的委托声明为弱属性?
-
不,如果我将代理设置为弱属性,我会收到以下错误:错误:“__weak 属性‘delegate’的现有实例变量‘_delegate’必须是__weak”
-
如果您的代表被保留,那么您有retain cycle。
-
谢谢,您知道如何在项目中避免循环吗?我认为这与我在视图控制器 B 中将对象加载到内存中的方式有关?
-
例如,不要保留代表。
标签: ios objective-c memory memory-management