你可以缓存 UIViewControllers 吗?
我有一个包含 8 个 UIViewController 的应用程序,通过从左/右/上/下导航呈现。它们是在应用程序启动时创建的,并保存在 NSArray 中。任何时候只有其中 1 个会添加到视图树 (addSubview:) - 其余的只是放在缓存中直到需要它们为止。
现在我遇到的问题是旋转到横向时。只有当前可见的视图才会将bounds.size更改为Landscape。当导航到下一个视图时,该视图仍然认为它是纵向的(但包含的视图都将看起来是横向的)。
顺便说一下,应用程序的视图层次结构如下:UIWindow ->主 UIViewController -> 8 个缓存的 UIViewController 中的 1 个。
例如: * 改变方向: - 主 UIViewController.view.bounds.size = 480x300(确定) - 缓存的 UIViewControllers 之一 view.bounds.size = 480x300 (OK) * 转到下一个视图: - 主UIViewController.view.bounds.size = 480x300(确定), - 另一个缓存的 UIViewControllers view.bounds.size = 320x460 (??)
不确定发生了什么。我是否必须以某种方式告诉缓存的 UIViewControllers 方向/大小发生了变化或其他什么?
多谢
I have an application with 8 UIViewControllers presented by navigating from left/right/up/down. They are created on start of the app and kept in an NSArray. Only 1 of them is added to the view tree (addSubview:) at any time - the rest just sit in the cache until they are needed.
Now the problem I am having is when rotating to Landscape. Only the currently visible view changes the bounds.size to Landscape. When navigating to the next view, that view still thinks it is in Portrait (but the containing views will all look in Landscape).
By the way, the view hierarchy of the app is the following: UIWindow -> Main UIViewController -> 1 of the 8 cached UIViewControllers.
For example:
* Change orientation:
- Main UIViewController.view.bounds.size = 480x300 (OK)
- One of the cached UIViewControllers view.bounds.size = 480x300 (OK)
* Go to next view:
- Main UIViewController.view.bounds.size = 480x300 (OK),
- Another of the cached UIViewControllers view.bounds.size = 320x460 (??)
Not sure whats going on. Do I have to tell the cached UIViewControllers somehow that the orientation/size changed or something else?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。最佳情况下,您应该在每个视图控制器离开屏幕时清除它管理的视图。
只需将
nil
分配给要替换的视图控制器的view
属性即可。这将释放无论如何不可见的视图使用的所有资源。作为一个额外的好处,每当您决定再次将特定的视图控制器放在前面时,都会使用适当的框架重新创建视图。我还假设您正在实现的是 UIViewController 的子类,它充当其他视图控制器的容器。就像名为
CWGridController
或类似的UITabController
的兄弟。注意,父视图控制器(您的子类)有责任根据需要调整其子视图控制器视图的框架大小。创建容器视图控制器并不是一项小任务,Apple 对此的支持并不完整。您必须做一些事情,包括但不限于:
让它发挥作用的最后一部分是打破一些规则。视图控制器工作得非常糟糕,除非它们知道其父视图控制器,布局将是错误的,并且模态视图控制器的行为很奇怪。不幸的是,
parentViewController
是只读的,至少你会这么认为。无论如何,使用 KVC 设置它可以解决大多数问题,并且当您提交到 App Store 时,Apple 似乎并不反对:Yes you can. Optimally you should purge the view that each view controller manages when it goes off screen.
Simply assign
nil
to theview
property of the view controller that is being replaced. This will free all resourced used by the view that is not visible anyway. As an added bonus the view is then recreated with proper frame whenever you decide to brin a particular viw controller in front again.I am also assuming that what you are implementing is a subclass of
UIViewController
that acts as a container for other view controllers. Like a sibling toUITabController
namedCWGridController
or similar. Noe that it is the responsibility of the the parent view controller (your subclass) to size the frame of it's child view controllers views as needed.Creating a container view controller is not a small task, and the support for doing it is not complete from Apple. There are a few things you must do including but not limited to:
The last part to make it work is to break some rules. View controllers works very badly unless they know about their parent view controller, layout will be wrong and modal view controller behaves strange. Unfortunately the
parentViewController
is readonly, or so you would think. Setting it anyway using KVC will solve most problems, and Apple does not seem to object when you submit to App Store:在 viewWillAppear 中,您必须检查界面方向并相应地设置框架。
In viewWillAppear you will have to check the interface orientation and set the frames accordingly.