iphone - applicationWillEnterForeground 内存泄漏

标签 iphone ios cocoa-touch uiviewcontroller uiapplicationdelegate

我的应用程序中有大约 9 个 ViewControllers。我正在使用 presentModalViewController 打开 ViewController。每次用户进入后台并进入前台时,我都想显示第一个 ViewController。如果用户在第 8 个 ViewController 中进入后台并再次打开应用程序,我需要显示第一个 View Controller。

我在 applicationWillEnterForeground

中使用以下代码
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    self.HomeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:@"HomeScreenViewController" bundle:nil];
    self.window.rootViewController = _homeScreenViewController;
    [self.window makeKeyAndVisible];
}

它显示内存泄漏。显然它会显示内存泄漏,因为我再次初始化 HomeCtrl,但我不知道如何修复它。谁能帮我 ?? 我没有使用 UINavigationController,因为 ViewController 是从右侧或左侧推送的。

最佳答案

改变:

self.HomeScreenViewController = [[HomeScreenViewController alloc] ....];

_HomeScreenViewController = [[HomeScreenViewController alloc] ....];

或者您可以执行以下代码:

HomeScreenViewController *tempHSVC = [[HomeScreenViewController alloc] ....];
self.HomeScreenViewController = tempHSVC;
[tempHSVC release];
self.window.rootViewController = self.HomeScreenViewController //or use just _homeScreenViewController;
[self.window makeKeyAndVisible];

并阅读了一些关于 objc 中内存管理的文章。

关于iphone - applicationWillEnterForeground 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11881731/

相关文章:

ios - 使 UIView 高度依赖于两个对象之间的负空间

iphone - 类别,协议(protocol),一大类?我应该在这里使用什么

iphone - 向 MKMapView 添加 subview (不是叠加层)

iphone - 使用 cornerradius IOS 使其变圆时发出相关的 UIimageView

iphone - 如何使标签加载网站上的内容?

ios - 使用视觉格式语言使 UIButton 之间的间距相等

ios - 不能将多个 UIBarButtonItem 添加到 RightBarButtonItems

iphone - UIView drawRect 绘图线宽度错误

ios - 尝试使用 AVFoundation 从前置摄像头捕获照片时出现 fatal error

iphone - UIToolBar - 禁用按钮