内存泄漏,多个UIViewControllers的其他问题

时间:2011-10-28 15:46:07

标签: ios ipad uiviewcontroller

我们在Xcode中开始使用基于Window的应用程序。在'AppDelegate'中我们有

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MainMenuViewController *mvc = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
    self.window.rootViewController = mvc;
    [mvc release];
    [self.window makeKeyAndVisible];
    return YES;
}

MainMenuViewController可以创建其他几个UIViewController派生类中的一个,这也允许用户返回主菜单。在MainMenuViewController中,我们有以下内容:

SecondLevelViewController* slvc = [[SecondLevelViewController alloc]initWithNibName:@"SecondLevelViewController" bundle:nil];
[self.view.window addSubview:slvc.view];
[self.view removeFromSuperview];

SecondLevelViewController有类似的代码返回主菜单。这样可行,但最终会在来回几次后创建两个类的实例,并且显然需要以其他方式完成。虽然Instruments没有报告任何内存泄漏,但应用程序的总内存使用量仍在增长,并且视图控制器的实时分配实例的数量也会增加。

我们认为对removeFromSuperview的调用会释放先前的视图控制器,但即使文档说它应该也不会发生这种情况。

我们还注意到需要调用release

SecondLevelViewController* slvc = [[SecondLevelViewController alloc]initWithNibName:@"SecondLevelViewController" bundle:nil];
[self.view.window addSubview:slvc.view];
[self.view removeFromSuperview];
[slvc release];  // < < < added this line

但结果是SIGABRTunrecognized selector sent to...

UINavigationViewController对我们来说不太适用,因为用户需要能够返回主菜单,无论他在菜单层次中有多深。

1 个答案:

答案 0 :(得分:1)

如果仍然有参考,它不是泄漏。尝试快照,有一个很棒的教程:bbum's weblog-o-mat

在:

[self.view.window addSubview:slvc.view];
[self.view removeFromSuperview];

您只是使用slvc来创建slvc.view,为什么不创建视图,因为不需要ViewController。

Re:UINavigationViewController不适合我们 你看过了吗:

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
相关问题