释放根视图控制器

时间:2013-05-10 12:48:22

标签: iphone ios xcode

我在

中有以下声明
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

陈述是:

root_view_controller = [[Root_View_Controller alloc] initWithNibName:@"Base_View" bundle : nil];

我没有使用ARC,所以我想在

中发布root_view_controller
- (void)applicationWillTerminate:(UIApplication *)application

我的问题是:上述做法是否正常?并且:在发布root_view_controller之前是否还应该添加其他清理代码?

2 个答案:

答案 0 :(得分:2)

AppDelegate.m

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[MLViewController alloc] initWithNibName:@"MLViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

如果你想发布你的Root_View_Controller你需要在dealloc方法中执行它,就像上面的代码一样

答案 1 :(得分:1)

无需在

中释放内存
- (void)applicationWillTerminate:(UIApplication *)application

因为当应用程序终止时,它所使用的内存无论如何都会被释放。