请清除一些关于 UIViewController 的混淆 [英] Please clear some confusions regarding UIViewController
问题描述
请澄清一些关于 UIViewController
我发现这篇文章Abusing UIViewController
,这里是链接link1 &link2
I found this article Abusing UIViewController
and here are the links link1 & link2
和总结要点
简而言之,这是作者(和 Apple 的)建议:
This is author's (and Apple’s) advice In a nutshell:
- 一个(并且只有一个)视图控制器应该负责 UIView 的整个层次结构(或全屏).
- 大多数情况下,每个屏幕应该只使用一个视图控制器.本质上,当前
UIWindow
的rootViewController
应该是唯一具有可见视图的UIViewController
. - 每个不同的屏幕应该有一个不同的视图控制器,即一个控制器不应控制多个屏幕.
- 您不应在视图层次结构中嵌套自定义
UIViewControllers
. - 如果有多个
UIViewController
挂在应用程序的UIWindow
上,则只有其中之一会收到有关方向更改的消息.其他人不会收到这些消息. - 嵌套的
UIViewControllers
不能保证或可能会收到有关方向或生命周期消息更改的消息,例如viewDidAppear:
、viewWillAppear:
,viewDidDisappear:
和viewWillDisappear:
即使它们继承自UIViewController
.只有最顶层的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.
和 apple docs 标题下的Coordinating Efforts between View Controllers
苹果说
and in apple docs under heading of Coordinating Efforts Between View Controllers
apple saying
很少有 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
层次结构中的特定视图?(apple doc 显示隐藏的东西).
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-per-screen 规则.从 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屋!