application:didFinishLaunchingWithOptions:内存管理 [英] application:didFinishLaunchingWithOptions: memory management
问题描述
我对内存管理有疑问.在我的应用程序委托中,我有以下方法;其中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屋!