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

PageViewController Disabled after pressing button with segue - Swift 4

我成功地实现了一个可以在五个页面之间滑动的 PageViewController (ViewControllers)。问题是当我添加一个 segues 到其中一个 ViewControllers 的按钮时。它显示了页面,就像它应该的那样,但后来我被困在那个页面上。 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]
   }
}

您可以将 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,
    ]
}()

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

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

并在 ViewController4

中调用 pageViewController?.scrollToViewController3()