UIView的addSubview是否真的保留了视图? [英] Does UIView's addSubview really retain the view?

查看:111
本文介绍了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];
}


推荐答案

>

This line:

[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.

这就是为什么苹果定义委托方法,如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是否真的保留了视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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