我该怎么做才能使我的页面视图控制器不是无止境的?

How do I make it so my page view controller isn't endless?

我有一个页面视图控制器,它允许我滚动浏览各种注册页面并输入必要的信息。问题是它只是在一个圆圈内滚动。如何才能让 PlayerInfo 页面成为我可以滚动到的最后一页?

lazy var SignupArray : [UIViewController] = {
    return [self.VCInstance(name: "ParkSelect"),
            self.VCInstance(name: "SportSelect"),
            self.VCInstance(name: "PlayerInfo")]
}()

private func VCInstance(name: String) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self
    self.delegate = self

    if let firstVC = SignupArray.first {
        setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)

    }
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
    guard let viewControllerIndex = SignupArray.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return SignupArray.last
    }

    guard SignupArray.count > previousIndex else {
        return nil
    }

    return SignupArray[previousIndex]
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
    guard let viewControllerIndex = SignupArray.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < SignupArray.count else {
        return SignupArray.first
    }

    guard SignupArray.count > nextIndex else {
        return nil
    }

    return SignupArray[nextIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
    guard let viewControllerIndex = SignupArray.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return nil
    }

    return SignupArray[previousIndex]
}


public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
    guard let viewControllerIndex = SignupArray.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1

    guard SignupArray.count > nextIndex else {
        return nil
    }

    return SignupArray[nextIndex]
}

之所以没完没了是因为

当 previousIndex 变为零时,您传递数组

中的最后一个 VC
guard previousIndex >= 0 else {
    return SignupArray.last
}

当 nextIndex 等于 signUp 数组的计数时,您传递数组中的第一个 VC。

guard nextIndex < SignupArray.count else {
    return SignupArray.first
}

删除这两个代码,您的 PageViewer 将停止

Return nil 而不是环绕。例如:

public func pageViewController(_ pvc: UIPageViewController, viewControllerAfter vc: UIViewController) -> UIViewController?{
    guard
        let i = SignupArray.index(of: vc),
        i < SignupArray.count - 1
    else {
        return nil
    }
    return SignupArray[i + 1]
}

或者,更简洁:

public func pageViewController(_ pvc: UIPageViewController, viewControllerAfter vc: UIViewController) -> UIViewController?{
    let i = 1 + (SignupArray.index(of: vc) ?? SignupArray.count)
    return i < SignupArray.count ? SignupArray[i] : nil
}