请清除有关UIViewController的一些困惑 [英] Please clear some confusions regarding UIViewController

查看:71
本文介绍了请清除有关UIViewController的一些困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请清除一些关于UIViewController

我找到了这篇文章Abusing UIViewController,这是链接链接1 & link2

I found this article Abusing UIViewController and here are the links link1 & link2

和总结要点

这是作者(和苹果公司)的建议:

This is author's (and Apple’s) advice In a nutshell:

  1. 一个(也只有一个)视图控制器应该负责UIViews的整个层次结构(或全屏显示).
  2. 通常,每个屏幕只能使用一个视图控制器.本质上,当前UIWindowrootViewController应该是唯一具有可见视图的UIViewController.
  3. 每个不同的屏幕都应具有不同的视图控制器,即一个控制器不应控制一个以上的屏幕.
  4. 您不应在视图层次结构中嵌套自定义UIViewControllers.
  5. 如果多个UIViewController挂在了应用程序的UIWindow上,则其中只有一个会收到有关方向更改的消息.其他人将不会收到这些消息.
  6. 即使嵌套UIViewControllersUIViewController继承,也不能保证或很可能会收到
  7. 嵌套的UIViewControllers消息,例如viewDidAppear:viewWillAppear:viewDidDisappear:viewWillDisappear:.当然,只有最高的UIViewController可以获取这些消息.
  1. One (and only one) view controller should be responsible for a whole hierarchy (or screenful) of UIViews.
  2. Mostly, you should only use one view controller per screen. Essentially the rootViewController of the current UIWindow should be the only UIViewController with a visible view.
  3. Each different screen should have a different view controller i.e. one controller should not control more than one screen.
  4. You should NOT nest custom UIViewControllers within a view hierarchy.
  5. If more than one UIViewController hangs off the application’s UIWindow, only one of these will get the messages for changes in orientation. The other(s) will NOT get these messages.
  6. Nested UIViewControllers are not guaranteed, or likely, to receive messages for changes in orientation or lifecycle messages such as viewDidAppear:, viewWillAppear:, viewDidDisappear: and viewWillDisappear: even though they inherit from UIViewController. Only the topmost UIViewController is certain to get these messages.

请清除第2点和第3点 因为当我们使用UINavigationControllerUITabBarController时,我们使用了UIViewController的多个子类.而ios设备只有一个屏幕.....

Please clear point number 2 and 3 because when we use UINavigationController or UITabBarController we use multiple subclasses of UIViewController. And ios device has only one screen.....

本文滥用UIViewController 突出了苹果的建议

注意:如果要将视图层次结构划分为多个子区域 并分别管理每个对象,使用通用控制器对象(自定义 从NSObject降级的对象),而不是视图控制器对象 管理每个分区.然后使用单个视图控制器对象 管理通用控制器对象.

Note: If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. Then use a single view controller object to manage the generic controller objects.

苹果文档Coordinating Efforts Between View Controllers苹果说

很少有iOS应用仅显示单个屏幕内容.相反,他们 首次启动时显示一些内容,然后显示和隐藏其他内容 内容以响应用户的操作.这些过渡提供了 单一的统一用户界面,可显示很多内容,但不会 一次全部.....

Few iOS apps show only a single screenful of content. Instead, they show some content when first launched and then show and hide other content in response to user actions. These transitions provide a single unified user interface that display a lot of content, just not all at once.....

我的要求是不要使用任何容器,模式或弹出窗口,我想进行手动管理,我有两个视图控制器VC1& VC2. VC1现在是根视图控制器,我想切换/转换/移动到VC2我应该怎么做?

My requirement is NOT to use any container or modal or popover, I want to do manual management, I have two view controllers VC1 & VC2. VC1 is the root view controller now I want to switch/transit/move to VC2 what should I do?

  1. VC1应该是UIViewController的唯一子类,而VC2应该是NSObject的子类,以管理VC1层次结构中的特定视图? .

  1. VC1 should be the only subclass of UIViewController and VC2 should be the subclass of NSObject to manage a particular view in VC1 hierarchy?(the show hide thing by apple doc).

VC2也可以是UIViewController的子类,我只是从根视图中删除VC1并将VC2添加为根视图?

VC2 can also be the subclass of UIViewController, I just remove VC1 from root view and add VC2 as root view?

或者正确的方法是什么?

or what is the correct way?

推荐答案

容器视图控制器(如UINavigationController)允许围绕每个屏幕一个VC规则工作.从iOS 5开始,开发人员已经能够并允许编写我们自己的容器控制器(实际上与普通VC并没有太大区别).通常,这意味着编写非VC控制器对象的必要性比以前要少.

Container view controllers (like UINavigationController) allow working around the one-VC-per-screen rule. Since iOS 5, developers have been able and allowed to write our own container controllers (which aren't actually much different from normal VCs). Generally this means that writing non-VC controller objects is less necessary than it used to be.

在您要替换根视图控制器的情况下,选项2更有意义.在可以使用的地方使用VC,仅在不能使用的地方使用非VC控制器对象.由于您要替换整个屏幕的内容,因此切换UIWindow rootViewController最为有意义(或者,许多开发人员都将使用导航控制器来呈现第二个视图,因为它简单又方便).

In your situation, where you want to replace the root view controller, your option 2 makes more sense. Use VCs where you can, and non-VC controller objects only when you can't. Since you're replacing the whole screen's content, just switching the UIWindow rootViewController makes the most sense (edit: alternately, many devs would just use a navigation controller to present the second view, because it's simple and convenient).

这篇关于请清除有关UIViewController的一些困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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