问题
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
I don't need to change the number of pages to be displayed, so can I avoid using setViewControllers
?
After reading through Xcode's page-based application template, I think this function may work:
func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> DataViewController?
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.
回答1:
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.
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.
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.
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)
More Info
Apple Docs
Stackoverflow
来源:https://stackoverflow.com/questions/45095539/how-to-jump-to-a-particular-page-with-uipageviewcontroller