gpt4 book ai didi

iphone - 使用 App Delegate 处理多个 UINavigationController 并确保 UINavigationController 正确解除分配

转载 作者:太空狗 更新时间:2023-10-30 03:37:10 24 4
gpt4 key购买 nike

我有一个应用程序使用两个 UINavigationControllers - 一个用于菜单系统,一个用于实际游戏。在我的 appDelegate 中声明了一个通用的 UINavigationController。当应用程序加载时,它会加载菜单或游戏的 UINavigationController。当然,玩家随后可以在两者之间导航。

当从菜单转到游戏时,我创建了一个新的 UINavigationController 并将其呈现如下:

    GameViewController *rootController = [[GameViewController alloc] init];
UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:rootController];
[rootController release];
newNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:newNavController animated:YES];
[newNavController release];

但是,我注意到当我这样做时,Menu 的 viewController 从不调用 dealloc。大概是因为仍然有一些东西让它活着。我发现,当我将 App Delegate 的 UINavigationController 明确设置为新的导航 Controller 时(在发布新的 navController 之前),它会发布菜单。我这样做如下:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
appDelegate.navController = newNavController;
[newNavController release];

这是好的做法吗?然而,我发现当从游戏导航回菜单时,同样的技巧似乎不起作用。 IE。

MainMenuViewController *menuViewController = [[MainMenuViewController alloc] init]; 
UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:menuViewController];
[menuViewController release];
newNavController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:newNavController animated:YES];

//Setting the appDelegate's navController to the new navController allows the menu to dealloc.
//This must happen AFTER the newNavController has been loaded.
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.navController = newNavController;
[newNavController release];

切勿在游戏的主 ViewController 上调用 dealloc。当我再次导航回游戏时,菜单的主 ViewController 也不再释放。

我在使用 UINavigationControllers 时是不是遗漏了什么?

谢谢,

迈克尔

编辑:我已经意识到我的游戏的主 ViewController 没有解除分配的原因是因为我有一些我没有失效的 NSTimers!但是,我仍然很想知道我的上述方法是否正确,并且在 App Delegate 中显式重新定义 navController 是允许不同的 UINavigationControllers 解除分配的正确方法:)

最佳答案

让您的生活更轻松。使用单个 UINavigationController,并维护两个单独的 View Controller 堆栈,它们只是 UIViewController 的数组。您可以使用 [UINavigationController setViewControllers:animated:] 来交换堆栈并将导航 Controller 留在原处。使用 UINavigationController.viewControllers 在替换之前获取当前堆栈。这比处理多个导航 Controller 的所有变幻莫测的事情要容易和清晰 143 亿倍。

关于iphone - 使用 App Delegate 处理多个 UINavigationController 并确保 UINavigationController 正确解除分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4233281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com