如何使用 UIPageViewController 跳转到特定页面? [英] How to jump to a particular page with UIPageViewController?

查看:42
本文介绍了如何使用 UIPageViewController 跳转到特定页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xcode 8 的默认基于页面的应用程序,但我一直在尝试跳转到特定页面(而不是向左和向右滑动来转动).我在 StackOverflow 上发现了类似的问题,但答案大多建议使用这种方法:

I'm using Xcode 8's default Page-based application, and I'm stuck trying to jump to a particular page (as opposed to swiping left and right to turn). I have found similar questions on StackOverflow, but the answers mostly suggested using this method:

 setViewControllers:direction:animated:completion

我不需要更改要显示的页数,那么我可以避免使用setViewControllers吗?

I don't need to change the number of pages to be displayed, so can I avoid using setViewControllers?

看了Xcode的基于页面的应用模板,我觉得这个功能可能有用:

After reading through Xcode's page-based application template, I think this function may work:

func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> DataViewController?

但是,我不知道从哪里获取参数 storyboard: UIStoryboard,因为 ModelController(用作 UIPageViewControllerDataSource 的控制器)不是故事板的一部分.

However, I don't know where to get the parameter storyboard: UIStoryboard, since ModelController (the controller that serves as UIPageViewControllerDataSource) isn't part of the storyboard.

推荐答案

传递给此方法的视图控制器是动画完成后可见的视图控制器.使用数据源提供额外的视图控制器供用户导航.

The view controllers passed to this method are those that will be visible after the animation has completed. Use a data source to provide additional view controllers to which users navigate.

在定义页面视图控制器接口时,您可以一次提供一个(或一次两个,取决于书脊位置和双面状态)或根据需要使用数据源提供内容视图控制器.一次提供一个内容视图控制器时,您可以使用setViewControllers(_:direction:animated:completion:)设置当前内容视图控制器的方法.要支持基于手势的导航,您必须使用数据源对象提供视图控制器.

When defining a page view controller interface, you can provide the content view controllers one at a time (or two at a time, depending upon the spine position and double-sided state) or as-needed using a data source. When providing content view controllers one at a time, you use the setViewControllers(_:direction:animated:completion:) method to set the current content view controllers. To support gesture-based navigation, you must provide your view controllers using a data source object.

页面视图控制器的数据源负责按需提供内容视图控制器,并且必须符合UIPageViewControllerDataSource协议.

The data source for a page view controller is responsible for providing the content view controllers on demand and must conform to the UIPageViewControllerDataSource protocol.

委托对象——符合UIPageViewControllerDelegate协议 - 提供一些与外观相关的信息并接收有关手势启动转换的通知.

The delegate object—an object that conforms to the UIPageViewControllerDelegate protocol—provides some appearance-related information and receives notifications about gesture-initiated transitions.

setViewControllers([<#Your ViewControllers#>], direction: .forward, animated: true, completion: nil)

更多信息

Apple 文档

Stackoverflow

这篇关于如何使用 UIPageViewController 跳转到特定页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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