简体   繁体   English

您可以缓存UIViewControllers吗?

[英]Can you cache UIViewControllers?

I have an application with 8 UIViewControllers presented by navigating from left/right/up/down. 我有一个通过从左/右/上/下导航呈现的具有8个UIViewControllers的应用程序。 They are created on start of the app and kept in an NSArray. 它们是在应用程序启动时创建的,并保存在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. 任何时候它们中只有1个会添加到视图树(addSubview :)中-其余的只是坐在缓存中,直到需要它们为止。

Now the problem I am having is when rotating to Landscape. 现在我遇到的问题是旋转到风景时。 Only the currently visible view changes the bounds.size to Landscape. 仅当前可见的视图将bounds.size更改为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. 顺便说一下,应用程序的视图层次结构如下:UIWindow-> Main UIViewController-> 8个缓存的UIViewControllers中的1个。

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 (??) 例如:*更改方向:-主UIViewController.view.bounds.size = 480x300(确定)-缓存的UIViewControllers其中之一view.bounds.size = 480x300(确定)*转到下一个视图:-主UIViewController.view.bounds .size = 480x300(OK),-另一个已缓存的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? 我是否必须以某种方式告诉缓存的UIViewControllers方向/大小已更改或其他?

Thanks a lot 非常感谢

Yes you can. 是的你可以。 Optimally you should purge the view that each view controller manages when it goes off screen. 最佳地,您应该清除每个视图控制器在离开屏幕时所管理的视图。

Simply assign nil to the view property of the view controller that is being replaced. 只需将nil分配给要替换的视图控制器的view属性。 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. 作为额外的好处,每当您决定再次将特定的viw控制器再次摆在前面时,便会以适当的框架重新创建视图。

I am also assuming that what you are implementing is a subclass of UIViewController that acts as a container for other view controllers. 我还假设您要实现的是UIViewController的子类,该子类充当其他视图控制器的容器。 Like a sibling to UITabController named CWGridController or similar. 就像UITabController的同级兄弟,名为CWGridController或类似名称。 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. 创建容器视图控制器不是一件容易的事,并且Apple并没有完成对容器视图控制器的支持。 There are a few things you must do including but not limited to: 您必须做一些事情,包括但不限于:

  • Forward all orientation change calls to all child view controllers. 将所有方向更改呼叫转发给所有子视图控制器。
  • Properly call all appear/disappear on child view controller as needed. 根据需要正确调用所有出现/消失的子视图控制器。

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. 不幸的是parentViewController是只读的,否则您会认为。 Setting it anyway using KVC will solve most problems, and Apple does not seem to object when you submit to App Store: 无论如何,使用KVC设置它都可以解决大多数问题,并且当您提交到App Store时,Apple似乎并不反对:

[childViewController setValue:self forKey:@"parentViewController"];

在viewWillAppear中,您将必须检查界面方向并相应地设置框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM