我有一个MultiView应用程序,并且有一些内存问题,希望能得到一些建议。
我有一个应用程序,该应用程序最初会加载一个开关控制器,使用户可以在某些视图之间进行切换。在应用程序中的某个时候,我想删除switchview控制器并向窗口添加另一个子视图。因此,我获得了访问共享应用程序的委托的权限,并删除了switchview控制器并插入了第二个。如果这是正确的方法,并且由于我打印了第二个控制器的retainCount值,恐怕会发生内存泄漏,它显示为19!
下面是我的代码的快照。这是正确的方法吗?如何避免这些内存泄漏?
好的,在我的ApplicationDelegate中,我有两个视图控制器,我也将它们设置为属性
MyAppDelegate.h
@class SwitchViewController;
@class SecondController;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
SwitchViewController *switchViewController;
SecondController *secondController;
}
@property (nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@property (nonatomic, retain) IBOutlet SecondController *secondController;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
在我添加的.m文件中
[self.window addSubview:switchViewController.view];
[self.window makeKeyAndVisible];
注意,我正在合成这些控制器,并在dealloc函数中释放它们
现在这是我的问题!在SwitchViewController.m中,我想要访问我的应用程序的委托,请删除当前的SwitchViewController并将其secondController放在窗口的子视图中:
SwitchViewController.m
SecondController *secondController2= [[SecondController alloc] init];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.switchViewController.view removeFromSuperview];
appDelegate.secondController = secondController2;
[appDelegate.window addSubview:appDelegate.secondController.view];
[secondController2 release];
这是问题。当我打印出[appDelegate.secondController keepCounter]时,我得到19。这是正确的方法吗?我真的有内存泄漏吗?
提前致谢,
安德烈亚斯
最佳答案
您的方法看起来不错,但是有更好的测试方法,而不仅仅是盯着它。使用Mac随附的仪器工具测试是否有泄漏。另外,作为旁注,还有一种更好的过渡方式
[UIView transitionFromView:appDelegate.switchViewController.view
toView:appDelegate.secondController.view
duration:1.0
options:UIViewAnimationOptionTransitionNone
completion:nil];
希望这可以帮助。