objective-c - 在 UIViewController 的 viewDidUnload 中释放 IBOutlets?

标签 objective-c iphone uiviewcontroller nib ios

我对基于 Nib 的 UIViewController 到底发生了什么感到有点困惑秒。生成 UIViewController 时子类,该模板在 viewDidUnload 中包含非常具体的注释方法:

// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

这适用于哪些 subview ?

  • 我在 viewDidLoad 中初始化的那些? (我会说是的)
  • 我在 initWithNibName 中初始化的那些? (我会说不)
  • 在 Nib 中引用对象的 IBOutlets?

如果我这样使用 View Controller :

MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

在这种情况下,我认为持有 subview 引用的实例变量是否在viewDidUnload 中被释放并不重要。或 deallocdealloc一旦 View Controller 从堆栈中弹出,就应该被调用,所以我不妨按照 Apple 所说的那样做,并在 viewDidUnload 上释放实例变量。而不是 dealloc .

但假设我使用了 MyViewController作为可以多次推送的实例变量:

if(self.myViewController == nil) {
    self.myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
}
//Change some state which should be reflected in the view
self.myViewController.someProperty = someValue;
[self.navigationController pushViewController:self.myViewController animated:YES];

MyViewController 中发生了什么如果我在 viewDidUnload 中发布一个 IBOutlet ?我可以指望在下一个 viewDidLoad 有一个新的引用吗? ?

换句话说:在 viewDidUnload 之后 View 本身发生了什么?如果再次按下 Controller ,它是否从 Nib 释放并重新加载?还是 View 保留在内存中?如果是这样, socket 是否在viewDidLoad之前重新设置? ?

如果 View 保留在内存中并且 socket 在 viewDidLoad 之前重新设置或每次按下 Controller 时都会重新加载,我想在 viewDidUnload 中释放 socket 是正确的(即使在第一种情况下无关紧要)。但否则(特别是如果 View 保留在内存中并且未重新设置 socket ),释放 viewDidUnload 中的 subview 。对于我提出的用例来说是错误的,我是正确的吗?

最佳答案

来自UIViewController documentation 、内存管理部分:

When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.

所以你不仅可以在viewDidUnload中释放你的outlets,这是首选和推荐的方式。

是的,您可以指望在调用 viewDidLoad 时让您的导出指向有效对象。再次来自 UIViewController 文档,关于 viewDidLoad:

This method is called after the view controller has loaded its associated views into memory.

关于objective-c - 在 UIViewController 的 viewDidUnload 中释放 IBOutlets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3184902/

相关文章:

ios - 来自另一个 View Controller 时的 xcode 问题

ios - 运行 Non-Storyboard 应用程序的运行时错误

objective-c - 使用 UITableView 向下钻取分层数据

iphone - 更改 UINavigationBar 的后退按钮

objective-c - 无法使用可达性 reachabilityForInternetConnection 检测互联网连接

iphone - 像 xcode 一样为 iphone/ipad 显示批量优化 PNG

iphone - ARC 警告 - "Not safe to remove an unused autorelease"

ios - UIViewController 基类——好不好

ios - 批量删除剩下的所有行改为向左和向上

objective-c - iOS json : put request with token