使用segue按下按钮后禁用PageViewController-Swift 4

时间:2018-10-08 02:02:33

标签: ios swift4 segue uipageviewcontroller

我成功实现了PageViewController,在其中可以在五个页面(ViewController)之间滑动。问题是当我添加一个按钮来选择一个ViewController时。它像预期的那样显示页面,但随后我被卡在该页面上。 PageViewController不再起作用。我无法在页面之间滑动。

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    lazy var subViewControllers:[UIViewController] = {
        return [
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController1") as! ViewController1,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as! ViewController2,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as! ViewController3,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController4") as! ViewController4,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController5") as! ViewController5,
        ]
    }()

    override func viewDidLoad() {
    super.viewDidLoad()

        self.delegate = self
        self.dataSource = self

        setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
    }

// MARK - UIPageViewControllerDataSource

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return subViewControllers.count
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if(currentIndex <= 0) {
            return nil
        }
        return subViewControllers[currentIndex-1]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if(currentIndex >= subViewControllers.count-1) {
            return nil
        }
        return subViewControllers[currentIndex+1]
   }
}

1 个答案:

答案 0 :(得分:0)

您可以将PageViewController的引用传递给子视图控制器,以便可以从子视图控制器调用PageViewController的方法。例如,创建

var pageViewController : PageViewController?
ViewController4

然后在subViewControllers

中分配它
lazy var subViewControllers:[UIViewController] = {
    let viewController4 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController4") as! ViewController4
    viewController4.pageViewController = self
    return [
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController1") as! ViewController1,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as! ViewController2,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as! ViewController3,
        viewController4,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController5") as! ViewController5,
    ]
}()

然后可以从PageViewController调用ViewController4的任何方法。为简单起见,您可以在scrollToViewController3()

中创建一个PageViewController方法
func scrollToViewController3()
{
    setViewControllers([subViewControllers[2]], direction: .reverse, animated: true, completion: nil)
}

并在pageViewController?.scrollToViewController3()中呼叫ViewController4