我有3个ViewControllers添加到UIPageViewContoller一切正常 但我的代码在这里显示第一个视图或数字0看起来像从0到2的演练我希望视图控制器首先吃午餐是数字1,这意味着中间的
class PageController: UIPageViewController, UIPageViewControllerDataSource {
lazy var viewControllerlist: [UIViewController] = {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let V1 = storyBoard.instantiateViewController(withIdentifier: "M")
let V2 = storyBoard.instantiateViewController(withIdentifier: "N")
let V3 = storyBoard.instantiateViewController(withIdentifier: "S")
return[V1, V2, V3]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
if let firstViewController = viewControllerlist.first {
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerlist.index(of: viewController) else {return nil}
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else {return nil}
guard viewControllerlist.count > previousIndex else {return nil}
return viewControllerlist[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerlist.index(of: viewController) else {return nil}
let nextIndex = vcIndex + 1
guard viewControllerlist.count != nextIndex else {return nil}
guard viewControllerlist.count > nextIndex else {return nil}
return viewControllerlist[nextIndex]
}
}
答案 0 :(得分:-1)
试试这个
let middle = viewControllerlist[1]
self.setViewControllers([middle], direction: .forward, animated: false, completion: nil)