When should I release [[UIApplication sharedApplication] delegate] object?(我应该何时释放 [[UIApplication sharedApplication] delegate] 对象?)
问题描述
我在我的应用程序中多次使用以下代码(尤其是用于管理 NavigationController):
I'm using the following code many times in my app (especially to manage a NavigationController):
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
我应该什么时候发布它?
When should I release it ?
感谢您的帮助,
斯蒂芬
推荐答案
不要.从不释放您的应用程序委托 - 它由操作系统自动管理.
Don't. Never release your application delegate - it is managed automatically by the OS.
如果您查看应用程序的 main.m 文件,您会看到一些代码初始化了代表您的应用程序的 UIApplication
实例 - 它负责管理应用程序委托的生命周期,而不是你的责任.
If you look in your app's main.m file, you'll see some code that initializes an instance of UIApplication
that represents your app - it is its responsibility to manage the application delegate's lifecycle, not your responsibility.
EDIT 正如@Goz 指出的那样,如果您在某个时候 retain
它,则应该 release
它.然而,由于应用程序对象(以及因此,它的委托)保证在应用程序的生命周期内保持在范围内(除非你去搞砸它),最好简单地 不对委托进行任何内存管理,因为这样可以避免意外过度释放或其他相关问题的可能性.
EDIT as @Goz points out, you should release
it if at some point you retain
it. However, since the application object (and therefore, by extension its delegate) is guaranteed to remain in scope for the life of the app (unless you go messing with it), it's far better to simply not do any memory management on the delegate, as this avoids the possibility of accidental over-release or other related issues.
这篇关于我应该何时释放 [[UIApplication sharedApplication] delegate] 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我应该何时释放 [[UIApplication sharedApplication] delegate] 对象?


基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01