UIView 的 addSubview 真的保留视图吗?

Does UIView#39;s addSubview really retain the view?(UIView 的 addSubview 真的保留视图吗?)

本文介绍了UIView 的 addSubview 真的保留视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况似乎并非如此.在下面的代码片段中,如果我删除该行:self.navigationController = nav,根控制器的视图将不会显示,这表明 addSubview 可能实际上不会像其他建议的那样保留视图.有什么想法吗?

I ran into a situation that seems to suggest otherwise. In the following code snippet, if I remove the line: self.navigationController = nav, the root controller's view won't show up, suggesting to me that addSubview might not actually retain the view as otherwise suggested. Any idea?

- (void)applicationDidFinishLaunching:(UIApplication *)application {   
   self.testViewController = [[TestViewController alloc] initWithNibName:@"TestView" bundle:  [NSBundle mainBundle]];

   UINavigationController *nav = [[UINavigationController alloc]  initWithRootViewController:self.testViewController];

   self.navigationController = nav;  //<-- if this line is removed, test view won't show up

   [window addSubview:nav.view];

   [nav release];
}

推荐答案

这一行:

[window addSubview:nav.view];

不会立即向屏幕添加视图.它由操作系统在可能不同的线程上的某个未来运行循环中显示.我们无法确定的实际实现.

does NOT add a view to the screen immediately. It is displayed by the OS in some future run loop on a possibly different thread. The actual implementation we can't be sure of.

这就是 Apple 定义像 viewDidAppear/viewWillAppear 这样的委托方法的原因,否则我们将不需要它们,因为我们可以准确地知道这些事件何时发生.

This is why Apple defines delegate methods like viewDidAppear/viewWillAppear, otherwise we would not need them as we would know precisely when these events occur.

此外,如您所说,添加子视图确实保留了视图.它确实但保留了视图控制器或导航控制器.由于导航控制器保留任何添加的视图控制器,我们不必用 ivar 支持它们.

Moreover, adding a subview as you said, does indeed retains the view. It does NOT however retain the view controller or the navigation controller. Since the navigation controller WILL retain any added view controllers, we do not have to back them with an ivar.

但是,您对导航控制器的引用必须持续超出该方法的范围.或者根据您的代码,它可能会被解除分配或丢失其引用.

But, your reference to the navigation controller must persist beyond the scope of the method. or depending on your code it could be dealloc-ed or have its reference lost.

因此,您必须使用 ivar 保留对导航控制器的引用并将其设置为:

So you must keep a reference to the navigation controller with an ivar and set it like so:

self.navigationController = nav; 

因此,即使 nav.view 包含一个指向 testViewController.view 的指针,应用程序也没有引用导航控制器,进而也没有引用视图.结果是一个空白屏幕.

So even though nav.view contains a pointer to testViewController.view, the application has no reference the navigation controller and, by extension, the view. The result is a blank screen.

为了更清楚地表明这不是保留/释放问题,您实际上是在以下方法中泄漏:

To make this more obvious that it isn't a retain/release problem, you are actually leaking in the following method:

self.testViewController = [[TestViewController alloc] initWithNibName:@"TestView" bundle: [NSBundle mainBundle]];

您需要通过以下方式自动释放以平衡您的保留/释放:

You need to autorelease to balance out your retain/releases by:

self.testViewController = [[[TestViewController alloc] initWithNibName:@"TestView" bundle: [NSBundle mainBundle]] autorelease];

因此,这意味着在您运行此代码时,您的视图从未被释放过.这进一步向我们保证,您的问题确实是一个丢失的参考.

So, that means your view has never, ever been deallocated any time you have ran this code. Which further assures us that your issue is indeed a lost reference.

这篇关于UIView 的 addSubview 真的保留视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIView 的 addSubview 真的保留视图吗?

基础教程推荐