使用我的View Controllers Swift 4中的按钮使用UIPageViewController更改页面

时间:2019-03-04 22:55:12

标签: ios swift uipageviewcontroller

我有一个UIPageViewController,让我在多个视图控制器之间导航。 我的应用程序可以在屏幕上滑动,但是我想删除滑动手势以在页面之间浏览视图内不同按钮 这是我在页面视图cxontroller上使用的代码

class RootPageViewController: UIPageViewController,UIPageViewControllerDataSource {

lazy var viewControllerList: [UIViewController] = {
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)

    let VC1 = storyBoard.instantiateViewController(withIdentifier: "orangeVC")
    let VC2 = storyBoard.instantiateViewController(withIdentifier: "blueVC")
    let VC3 = storyBoard.instantiateViewController(withIdentifier: "purpleVC")

    return [VC1, VC2, VC3]
}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self

    if let firstViewController = viewControllerList.first {
        self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)            
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

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 :(得分:0)

您只需要在按钮操作内添加此行

self.setViewControllers([viewControllerList[indexHere]], direction: .forward, animated:false, completion: nil)