application:didFinishLaunchingWithOptions:内存管理 [英] application:didFinishLaunchingWithOptions: memory management

查看:76
本文介绍了application:didFinishLaunchingWithOptions:内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对内存管理有疑问.在我的应用程序委托中,我有以下方法;其中welcomeViewController是一个ivar.

I have a question about the memory management. In my app delegate, I have the following method; where welcomeViewController is an ivar.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    welcomeViewController = [[CBWelcomeViewController alloc] init];
    UINavigationController *appNavigationController = [[UINavigationController alloc] initWithRootViewController:welcomeViewController];
    [self.window addSubview: [appNavigationController view]];
    [self.window makeKeyAndVisible];
    return YES;
}

要释放welcomeViewController的内存,我只需在dealloc方法中对其调用release.

To release the memory for welcomeViewController, I simply call release on it in the dealloc method.

- (void)dealloc {
    [welcomeViewController release];
    [window release];
    [super dealloc];
}

我的问题是,管理appNavigationController内存的正确方法是什么?

My question is, what is the correct way to manage the memory of appNavigationController?

推荐答案

您应将appNavigationController设置为实例变量,并在dealloc中将其设置为release.

You should make appNavigationController an instance variable and release it in dealloc.

您不需要将welcomeViewController作为实例变量,恰恰相反. 只需分配/初始化它,然后将其传递给UINavigationController,然后将其保留,然后立即release.

You do not need to have welcomeViewController as an instance variable, quite the opposite. Simply alloc/init it, then pass it off to the UINavigationController, which then retains it, then immediately release it.

这篇关于application:didFinishLaunchingWithOptions:内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆