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

问题描述 投票:-1回答:1

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

}

ios swift uipageviewcontroller
1个回答
0
投票

您只需要在按钮操作中使用此行

self.setViewControllers([viewControllerList[indexHere]], direction: .forward, animated:false, completion: nil)            
© www.soinside.com 2019 - 2024. All rights reserved.