代码之家  ›  专栏  ›  技术社区  ›  Alexander

如何使我的页面视图控制器不是无止境的?

  •  0
  • Alexander  · 技术社区  · 8 年前

    我有一个页面视图控制器,允许我滚动各种注册页面并输入必要的信息。问题是它只是在一个圆圈里滚动。如何使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 回复  |  直到 8 年前
        1
  •  0
  •   Sandeep Bhandari    8 年前
    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]
    }
    

    它之所以无尽是因为

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

    当nextIndex等于注册数组的计数时,u传递数组中的第一个VC。

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

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

        2
  •  0
  •   rob mayoff    8 年前

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