请清除有关UIViewController的一些困惑 [英] Please clear some confusions regarding 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:
- 一个(也只有一个)视图控制器应该负责UIViews的整个层次结构(或全屏显示).
- 通常,每个屏幕只能使用一个视图控制器.本质上,当前
UIWindow
的rootViewController
应该是唯一具有可见视图的UIViewController
. - 每个不同的屏幕都应具有不同的视图控制器,即一个控制器不应控制一个以上的屏幕.
- 您不应在视图层次结构中嵌套自定义
UIViewControllers
. - 如果多个
UIViewController
挂在了应用程序的UIWindow
上,则其中只有一个会收到有关方向更改的消息.其他人将不会收到这些消息.
即使嵌套 - 嵌套的
UIViewControllers
消息,例如viewDidAppear:
,viewWillAppear:
,viewDidDisappear:
和viewWillDisappear:
.当然,只有最高的UIViewController
可以获取这些消息.
UIViewControllers
从UIViewController
继承,也不能保证或很可能会收到- One (and only one) view controller should be responsible for a whole hierarchy (or screenful) of UIViews.
- Mostly, you should only use one view controller per screen. Essentially the
rootViewController
of the currentUIWindow
should be the onlyUIViewController
with a visible view. - Each different screen should have a different view controller i.e. one controller should not control more than one screen.
- You should NOT nest custom
UIViewControllers
within a view hierarchy. - If more than one
UIViewController
hangs off the application’sUIWindow
, only one of these will get the messages for changes in orientation. The other(s) will NOT get these messages. - Nested
UIViewControllers
are not guaranteed, or likely, to receive messages for changes in orientation or lifecycle messages such asviewDidAppear:
,viewWillAppear:
,viewDidDisappear:
andviewWillDisappear:
even though they inherit fromUIViewController
. Only the topmostUIViewController
is certain to get these messages.
请清除第2点和第3点
因为当我们使用UINavigationController
或UITabBarController
时,我们使用了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?
-
VC1
应该是UIViewController
的唯一子类,而VC2
应该是NSObject
的子类,以管理VC1
层次结构中的特定视图? .
VC1
should be the only subclass ofUIViewController
andVC2
should be the subclass ofNSObject
to manage a particular view inVC1
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屋!