我怎么做到这样我的页面视图控制器不是无穷无尽的?

时间:2017-09-14 16:26:04

标签: ios swift uipageviewcontroller

我有一个页面视图控制器,允许我滚动各种注册页面并输入必要的信息。问题是它只是滚动一圈。如何使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]
}

2 个答案:

答案 0 :(得分:0)

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数组时,u通过数组中的第一个VC。

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

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

答案 1 :(得分:0)

返回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
}