是否可以将UIViewController的视图添加到另一个UIViewController的视图中? [英] Is it advisable to add a UIViewController's view to another UIViewController's view?

查看:280
本文介绍了是否可以将UIViewController的视图添加到另一个UIViewController的视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议将 UIViewController 视图添加到另一个 UIViewController 查看

Is it advisable to add a UIViewController's view to another UIViewController's view?

请解释为什么这是一种良好做法或不良做法。

Please explain why it is a good practice or a bad practice.

推荐答案

Apple以及大多数遵循Apple指南的人会告诉你这是不好的做法,并且Apple为此添加了ViewController包含(childViewControllers)原因。不幸的是,大多数人盲目地遵循这一点,并不会告诉你为什么这是不好的做法。我很高兴你这么做。

Apple and thus most people following Apple's guidelines will tell you it's bad practice and that Apple added ViewController containment (childViewControllers) for that reason. Unfortunately, most people blindly follow that and won't tell you why it is bad practice. And I'm glad that you do ask that.

事实上,在模型 - 视图 - 控制器架构中,视图应该是可重用的,无论它们包含什么内容,因此控制视图内容的视图和对象不应该相同。这正是UIViewController在iOS5之前所做的事情。苹果不鼓励你多次使用它们,而这是非常合乎逻辑的事情。当然这很令人困惑,许多人忽略了指导方针,无论如何都做了,包括我自己,应用程序工作正常并通过了应用商店验证,这引起了更多的混乱。
结果是,直到今天,人们仍然在Apple塌陷并给我们自定义容器ViewControllers一年多后就此问题。我已经看到人们经常使用复杂的答案来回答这个问题,只要将UIViewController重新创建为一个继承自NSObject的类来解决非常简单的问题。仅仅因为Apple不鼓励使用UIViewControllers,甚至不知道为什么。

The truth is, in a model-view-controller architecture, views should be reuseable regardless of the content they contain, thus the view and the object controlling the view's content should not be the same. Which is exactly what a UIViewController does yet prior to iOS5 Apple discouraged you to use them in multiples while it's a very logical thing to do. Ofcourse this was confusing, many people ignored the guidelines and did it anyway, myself included, apps worked fine and passed app store validation which caused even more confusion. The result is that to this day people still ask questions about this even more than a year after Apple caved and gave us custom container ViewControllers. I've seen people respond to this question often with complex answers as far as recreating UIViewController as a class that inherits from NSObject for very simple issues. Just because Apple discourages using UIViewControllers and without even knowing why.

由于将ViewController的视图添加为子视图通常可以完美地工作,并且iOS4中的ViewController包含不可用支持,太多人不打扰使用ViewController遏制。这是更清洁的解决方案,当您想在ViewControllers中使用ViewControllers时,您应该尽可能使用它。如果没有,在大多数情况下你应该能够简单地将ViewController的视图添加为子视图,你只需要知道在哪些情况下。

Since adding a ViewController's view as a subview will often work perfectly and ViewController containment is not available in iOS4 which many still support, too many people don't bother using ViewController containment. It is the cleaner solution and when you feel like using ViewControllers in ViewControllers, you should use it when possible. If not, in most situations you should be able to simply add a ViewController's view as a subview, you just have to know in which situations.

这是你可以期待的您只需将ViewController的视图添加到另一个视图:

Here's what you can expect if you just add a ViewController's view to another view:


  • 视图回调方法无法保证被调用。 viewWillAppear,viewDidAppear,viewWillDisappear和viewDidDisappear等方法可能会被调用,也可能不会被调用。它在很大程度上取决于操作系统版本,在iOS4中它们永远不会被调用,在iOS5及以上版本中,它们通常会被调用。所以你不能覆盖这些方法,因为你不能依赖它们,你无法控制它们被调用的时间,次数或次数。

  • 唯一的视图回调将始终被正确调用的方法是viewDidLoad。

  • 不会调用旋转回调。根据您的情况,这可能是一个大问题或根本不重要。如果视图的autoresizingmask足以重新定位并重新缩放它,那么你就没事了。如果没有,你可以随时调用superview的ViewController的旋转回调来进行自定义实现。

  • 你必须自己保持对ViewController的引用,否则它会立即释放视图仍将由其超级视图保留。

  • View callback methods are not guaranteed to get called. Methods like viewWillAppear, viewDidAppear, viewWillDisappear and viewDidDisappear may or may not get called. It will largely depend on the OS version, in iOS4 they won't ever get called, in iOS5 and above they'll mostly get called. So you can't override these methods because you can't rely on them, you have no control over when, if or how many times they'll get called.
  • The only view callback method that will always get called correctly is viewDidLoad.
  • Rotation callbacks won't get called. Depending on your situation this may be a big deal or not matter at all. If the view's autoresizingmask is enough to reposition and rescale it then you're fine. If not, you can always do a custom implementation when the superview's ViewController's rotation callbacks get called.
  • You have to keep a reference to the ViewController yourself, otherwise it'll get released immediately while its view will still be retained by its superview.

我绝对不会鼓励它,但我也不鼓励它。这是情境化的,如果你不再需要支持iOS4那么你可以避免它。但是,如果你记住上面的列表,那么它也不会造成任何伤害,你的应用程序将正常工作。

I definitely wouldn't encourage it but I don't discourage it either. It's situational and if you don't have to support iOS4 anymore then you can mostly avoid it. But if you keep the above list in mind then it won't cause any harm either and your app will work fine.

这篇关于是否可以将UIViewController的视图添加到另一个UIViewController的视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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