加载应用时,首先显示pageViewController num的中间页面

时间:2018-02-09 22:00:42

标签: ios swift uipageviewcontroller

我有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]
    }

}

1 个答案:

答案 0 :(得分:-1)

试试这个

 let middle = viewControllerlist[1]
 self.setViewControllers([middle], direction: .forward, animated: false, completion: nil)