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