通过 addSubiew 添加的子视图 viewcontroller 是否需要 dealloc?
通常,当我使用将当前视图控制器推开的视图控制器时,我使用 UINavigationController 并推送/弹出新的视图控制器,并让它们自己处理所有的 dealloc。
但是,例如,在本例中,我有一个 MainViewController,它是应用程序启动时的默认视图。我有第二个视图,称为SecondaryViewController,它是主屏幕上的弹出窗口(有点像灯箱)。
下面是用于说明的代码:
//From within mainViewController:
secondaryViewController = [SecondaryViewController alloc] initWithNibName:@"SecondaryViewController" bundle:nil];
[self.view addSubview:secondaryViewController.view];
secondaryViewController 界面如下所示:
//interface
@interface SecondaryViewController : UIViewController
{
IBOutlet UILabel *httpLabel;
IBOutlet UIScrollView *scrollView;
}
@property(retain, nonatomic) IBOutlet UILabel *httpLabel;
@property(retain, nonatomic) IBOutlet UIScrollView *scrollView;
至于实现,我有 @property
ivars 的 @synthesize
,但我没有这样做任何手动分配。但是,我确实放置了一个 dealloc 方法:
- (void)dealloc
{
[httpLabel release];
[scrollView release];
[super dealloc];
}
但我不确定我是否需要上述方法。
所以我的问题如下:
1)在这种情况下我需要上述的 dealloc 方法吗?或者更一般地说,子视图什么时候需要 dealloc 方法?
2)如果我需要或不需要它,这取决于我是否通过addSubview或pushViewController添加 secondaryViewController ?例如,如果我想用以下内容替换整个 mainViewController:
[self.navigationController pushViewController:secondaryViewController animated:NO]
secondaryViewController 需要 dealloc 方法吗?
谢谢你!
normally when I'm using a viewcontroller that will push the current viewcontroller out of the way, I use a UINavigationController and push/pop the new viewcontrollers and let them handle all the dealloc themselves.
However, for example, in this case, I have a MainViewController, which is the default view when the app starts up. I have a second view, called SecondaryViewController, that is a popup on the main screen (sort of like a lightbox).
Here is the code to illustrate:
//From within mainViewController:
secondaryViewController = [SecondaryViewController alloc] initWithNibName:@"SecondaryViewController" bundle:nil];
[self.view addSubview:secondaryViewController.view];
The secondaryViewController interface looks like this:
//interface
@interface SecondaryViewController : UIViewController
{
IBOutlet UILabel *httpLabel;
IBOutlet UIScrollView *scrollView;
}
@property(retain, nonatomic) IBOutlet UILabel *httpLabel;
@property(retain, nonatomic) IBOutlet UIScrollView *scrollView;
As for the implementation, I have the @synthesize
for the @property
ivars, but I'm not doing any manual allocs. However, I did put a dealloc method:
- (void)dealloc
{
[httpLabel release];
[scrollView release];
[super dealloc];
}
But I'm not sure I need the above.
So my questions would be the following:
1) Do I need the above dealloc method in this case? Or more generally, when would a subview need a dealloc method?
2) If I do or dont need it, does it depend on whether I'm adding the secondaryViewController via addSubview or pushViewController? For instance, if I wanted to replace the entire mainViewController, with this:
[self.navigationController pushViewController:secondaryViewController animated:NO]
Would the secondaryViewController need a dealloc method?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,在这种情况下,您确实需要完全一样的 dealloc 方法。您走在正确的轨道上,因为您假设由于您没有进行任何手动分配,因此不需要进行任何释放/释放...但是,通过将属性指定为 (retain, nonatomic),您可以做隐式保留。
这意味着,如果您设置了这些属性,实际上在幕后发生的事情是这样的:
正如您所看到的,您的合成导致对象上发生保留。如果你从来不平衡保留和释放,它就会泄漏。因此,放置它的唯一合乎逻辑的位置是在您的 dealloc 方法中。这就创造了生命的循环。
如果您从未设置这些属性并且在 dealloc 中没有释放,那么它不会泄漏任何内容,但您显然不想做出这些假设。
如果您没有任何保留属性或任何手动分配的 ivars,那么也只有这样,您才能取消 dealloc 方法。
希望有帮助。
Yes, you do need the dealloc method exactly as you have it, in this case. You are on the right track because you're assuming that since you are not doing any manual allocating, you don't need to do any dealloc/releasing... however, by specifying the property as (retain, nonatomic), you are doing implicit retaining.
This means that if you ever set those properties, what's actually occurring under the covers is something like this:
As you can see, your synthesize is causing a retain to occur on an object. If you never balance that retain out with a release, it will leak. So the only logical place to put it, is in your dealloc method. This creates the circle of life.
If you never set these properties and don't have release in dealloc, then it won't leak anything, but you obviously wouldn't want to make those assumptions.
If you didn't have any retain properties or any manual allocing of ivars, then and only then, can you nuke the dealloc method.
Hope that helps.
我认为这在最新的 iOS 5+ 中是允许的,但以前你不应该向主视图控制器添加另一个视图控制器视图。这是明显的滥用,可能会导致问题。
视图控制器的概念是控制所有视图。视图控制器不应该控制另一个视图控制器,除非它是容器视图控制器,例如 UINavigationController/UITabBarController。
所以请重新考虑设计。为什么需要SecondaryViewController。为什么主视图控制器不能同时管理辅助视图?
最后,每个视图控制器都应该有 dealloc。
I think this is allowed in the latest iOS 5+ but previously you were not supposed to add another viewcontrollers view to your main viewcontroller. This is clear misuse and can lead to issues.
The concept of viewcontroller is one who controls all the views. A view controller should not control another viewcontroller unless it is a container viewcontroller such as UINavigationController/UITabBarController.
So please rethink the design. why do you need the SecondaryViewController. Why cannot the mainviewcontroller manage the secondary view as well?
Lastly, every viewcontroller should have the dealloc in it.
如果在将视图添加到层次结构后需要从主视图控制器访问 secondaryViewController,则此时不应取消分配它。如果在显示辅助控制器后不需要访问它,则可以在此时取消分配它。
实际上,如果 secondaryViewController 是 ivar,则保留对其的保留引用可能是有意义的。如果它是局部变量并且您以后不再访问它,则应该释放它。
If you need to access
secondaryViewController
from your main view controller after you've added its view to the hierarchy, you should not deallocate it at that point. If you don't need to access the secondary controller after you've displayed it, you can dealloc it at that point.In practical terms, if
secondaryViewController
is an ivar, it probably makes sense to keep a retained reference to it. If it's a local variable and you're not accessing it later, you should dealloc it.