UIView:添加 UIViewController 的视图作为子视图及其删除
我想问添加和删除 UIViewController 的视图作为子视图的正确方法是什么。 因此,初始化 UIViewController 后,我可以将其视图添加到视图层次结构中,如下所示:
UIViewController *myViewControler = [[UIViewController alloc] init];
[someAnotherView addSubview:myViewController.view];
问题 1:我应该在 addSubview:
调用后释放 myViewController.view 吗?
如果我想从视图层次结构中删除 myViewController 的视图,我会调用 [myViewController.view removeFromSuperview];
问题 2:在这种情况下,在视图removeFromSuperview 后,我应该如何释放 myViewController 实例?
I would like to ask what is the correct way to add and remove UIViewController's view as a child view.
So, having UIViewController initialized I can add its view to view hierarchy as follows:
UIViewController *myViewControler = [[UIViewController alloc] init];
[someAnotherView addSubview:myViewController.view];
Question 1: Should I release myViewController.view after addSubview:
call?
If I want to remove myViewController's view from view hierarchy I call [myViewController.view removeFromSuperview];
Question 2: How should I release myViewController instance in this case after its view removedFromSuperview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要释放视图,拥有的视图控制器将为您执行此操作。
我通常将 myViewController 的声明放在标头中,然后在完成后释放并将其归零(在正常流程中的某个位置或在包含视图控制器的 dealloc 中)。
You do not need to release the view, the owning view controller will do this for you.
I normally put the declaration of myViewController in the header and then release and nil it when I am done with it (either somewhere in the normal flow or in the dealloc of the containing view controller).