[英]UIPageViewController and setViewControllers:
I am having trouble understanding this line of code when I setup my UIPageViewController: 设置UIPageViewController时,我很难理解这一行代码:
[self.pageViewController setViewControllers:@[startingViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^(BOOL finished) {
// Completion code
}];
In most of the examples I have seen just a single UIViewController is passed into the setViewControllers and then the next one initiated using (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
. 在大多数示例中,我仅看到一个UIViewController传递到setViewControllers中,然后使用(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
初始化下一个(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
。 Why do we not init all the view controllers first and then pass them all into setViewControllers ? 为什么不先初始化所有视图控制器,然后再将它们全部传递给setViewControllers呢?
Any pointers on this would be great, thanks 对此,任何指点都会很棒,谢谢
Why do we not init all the view controllers first and then pass them all into setViewControllers ? 为什么不先初始化所有视图控制器,然后再将它们全部传递给setViewControllers呢?
Because that isn't how a UIPageViewController works. 因为那不是UIPageViewController的工作方式。 It has, at every moment, either one page at a time or two pages at a time (shown simultaneously , like a physical book lying open). 它每时每刻要么一页,要么一次两页( 同时显示,就像一本物理书一样打开)。 Most people use it to show one page at a time, so when you call setViewControllers:
, you supply that one page. 大多数人都使用它一次显示一页,因此,当您调用setViewControllers:
,就会提供该一页。
If you wanted to "init all the view controllers first", you wouldn't be using a UIPageViewController in the first place - you'd be using an ordinary UIScrollView. 如果您想“首先初始化所有视图控制器”,那么您将不会首先使用UIPageViewController,而是会使用普通的UIScrollView。 But that would be extremely heavyweight. 但这将是极其沉重的。 Suppose (as is in fact the case in one of my apps) there were two thousand pages. 假设(实际上是我的一个应用程序中的情况)有两千页。 Two thousand view controllers all at once??? 一次有两千个视图控制器??? You'd run out of memory at startup! 您在启动时会耗尽内存! The whole point of a UIPageViewController is that it is lightweight because, generally speaking, only one child view controller exists at a time. UIPageViewController的全部要点在于它是轻量级的,因为通常来说一次只存在一个子视图控制器。 It is dynamic , not static . 它是动态的 ,而不是静态的 。
The setViewControllers method can be thought of as setting the current page. 可以将setViewControllers方法视为设置当前页面。 The documentation tells you to pass in a certain number of viewControllers to this method depending on how many pages are shown at a time with the given settings. 该文档告诉您将一定数量的viewController传递给此方法,具体取决于一次使用给定设置显示的页面数。
From there you can implement viewControllerAfterViewController and viewControllerBeforeViewController to provide any other pages you want to display when the user swipes. 从那里,您可以实现viewControllerAfterViewController和viewControllerBeforeViewController来提供您想要在用户滑动时显示的其他页面。
As to why they implemented it like this, I'd say so you can choose the next page dynamically and because the lazy loading could have huge space benefits. 至于为什么要这样实现,我想说的是,您可以动态选择下一页,因为延迟加载可以带来巨大的空间收益。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.