有关View Controller视图的问题 [英] Question on View Controller's view

查看:47
本文介绍了有关View Controller视图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们访问View Controller的view属性时,它是始终代表整个屏幕还是部分屏幕?

When we access View Controller's view property, does it always represent the entire screen or it can be partial?

间接地说,View Controller是否可以具有多个视图属性?

Indirectly speaking, does a View Controller can have multiple view properties ?

推荐答案

donkim是正确的,但不要相信我的话;)

donkim is right but don't take my word for it ;)

请参阅Rincewind发表的帖子: https://devforums.apple.com/thread/81930以及其他Apple员工在其论坛上发表的许多旧评论.

See this post by Rincewind: https://devforums.apple.com/thread/81930 and any number of older comments from other Apple employees on their forums.

如果我了解你是什么 说,您创建了另一个视图 控制器(BlueViewController)和 将其视图添加为另一个视图的子视图 视图控制器?如果是这样,那么这是 文档涵盖的内容-专门针对 说不这样做,因为它不是 受支持.
视图控制器是 旨在管理整个屏幕的 值得的内容.如果你想 进一步细分您的内容,然后 建议您使用对象 除了a的子类 UIViewController来管理那些 分区并管理这些对象 带有UIViewController子类.

If I'm understanding what you are saying, you've created another view controller (BlueViewController) and added its view as a subview of another view controller? If so, then this is covered by the docs – specifically to say not to do that because it is not supported.
A view controller is meant to manage an entire screen's worth of content. If you want to subdivide your content further, then it is recommended that you use objects other than subclasses of a UIViewController to manage those subregions and manage those objects with a UIViewController subclass.

此外,我们还有 UIViewController类参考,其中指出:

In addition we have the UIViewController class reference which states:

您使用UIViewController的每个实例来管理视图层次结构.典型的视图层次结构包括一个根视图(该类的view属性中提供对其的引用)以及通常一个或多个表示实际内容的子视图.在iPhone和iPod touch上,根视图通常填充整个屏幕,但在iPad上,此视图可能仅填充屏幕的一部分.在这两种情况下,视图控制器均负责管理整个视图层次结构,包括所有子视图.

You use each instance of UIViewController to manage a view hierarchy. A typical view hierarchy consists of a root view—a reference to which is available in the view property of this class—and usually one or more subviews presenting the actual content. On iPhone and iPod touch, the root view typically fills the entire screen but on iPad this view may fill only part of the screen. In both cases, the view controller is responsible for managing the entire view hierarchy, including all subviews.

,以及适用于iOS的View Controller编程指南:

根视图充当与视图控制器关联的所有其他视图的容器.通常,您需要为此视图定义框架以匹配应用程序窗口的大小,该窗口本身应填满整个屏幕.但是,视图控制器还根据需要调整框架大小,以适应各种视图的存在,例如系统状态栏,导航栏或选项卡栏.

The root view acts as the container for all other views associated with your view controller. You typically define the frame for this view to match the size of the application window, which itself should fill the screen. However, the view controller also adjusts the frame size as needed to accommodate the presence of assorted views, such as the system status bar, a navigation bar, or a tab bar.

不幸的是,这些都不像我想要的那样明确.为什么 UIViewController做出这些假设以及何时可以安全地打破它们.据我所知,在没有访问UIKit代码的情况下,UIKit假定单个视图控制器将负责给定屏幕上的内容.因此,在某些情况下,如果您在一个窗口中可以看到多个视图控制器,则它仅向一个(或可能只有一些)视图控制器发送-viewWillAppear:或-didRecieveMemoryWarning之类的消息.尽管我最近还没有测试所有这些方法,但是应该至少可以以几种不同的方式来证明这一点,因此我要解决其他用户报告的问题. 嵌套视图控制器将不会收到-didReceiveMemoryWarning 如果在显示模态视图控制器时发生旋转,则多视图控制器的视图将不会旋转,然后取消模态. 有时希望视图控制器的视图充满整个窗口并且是不透明的,因此在未覆盖的窗口区域中绘制的内容可能是不确定的.至少在iOS 3.1.x或我尝试使用的最后一个版本中,尝试呈现无法填充屏幕的模式视图控制器都无法渲染基础视图.

Unfortunately none of those are as explicit as I would like about why UIViewController makes these assumptions and when it might be safe to break them. From what I can tell without access to UIKit code somewhere UIKit assumes a single view controller will be responsible for a given screen worth of content. In some cases it therefore sends messages like -viewWillAppear: or -didRecieveMemoryWarning to only one (or maybe only some) view controllers if you have multiple view controllers visible in a single window. It should be possible to demonstrate this in at least a couple of different ways though I haven't tested all of these recently so I'm going off of other user's reported problems. Nested view controllers will not receive -didReceiveMemoryWarning Multiple view controller's view will not be rotated if rotation occurs while a modal view controller is presented and the modal is then dismissed. View controllers' views are sometimes expected to fill the window and be opaque so the content drawn in regions of the window they do not cover can be undefined. Trying to present a modal view controller which did not fill the screen would not render underlying views, at least in iOS 3.1.x or whatever the last version I tried was.

尝试直接回答hmthur的问题. UIViewController应该具有单个根视图(其"view"属性),该根视图填充了窗口,并且可以处理调整后的大小以适合状态栏,选项卡栏等.该控制器可以具有该根视图的任意数量的子视图以及引用这些子视图的属性.

To try to directly answer hmthur's question. A UIViewController should have a single root view (its 'view' property) which fills the window and which can handle being resized to fit status bars, tab bars, and so on. That controller may have any number of sub views of that root view and properties which reference those sub views.

作为进一步更新,请考虑以下主题上的"创建容器UIViewController子类?"上的该线程Apple开发人员论坛(讨论设置parentViewController以便构建自定义容器视图控制器).

As a further update consider this thread on "Creating container UIViewController subclasses?" on the Apple developer forums (discussing setting parentViewController in order to build custom container view controllers).

所有UIKit ivar均被视为私有,并且不支持直接访问,包括通过setValue:forKey:的访问.设置此ivar的方法也是私有的.

All UIKit ivars are considered private and their direct access is unsupported, including access through setValue:forKey:. The method that sets this ivar is also private.

对于这个ivar [parentViewController],我会警告您它的行为可能会改变.

As for this ivar [parentViewController], I will warn you that its behavior is likely to change.

不幸的是,我没有其他好的建议,尽管值得一提的是,我们确实认识到创建容器视图控制器的困难.

Unfortunately I don't have any good suggestions for alternatives, though for what it's worth we do recognize the difficulty in creating container view controllers.

这篇关于有关View Controller视图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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