Search code examples
iosswiftuipageviewcontroller

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


I have a page view controller that allows me to scroll through the various signup pages and enter the necessary information. The problem is it just scrolls in a circle. How do I make it so that the PlayerInfo page is the last page I can scroll to?

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]
}

Solution

  • 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]
    }
    

    The reason it is endless because

    when previousIndex becomes zero you pass the last VC in array

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

    when nextIndex is equal to count of signUp array u pass the first VC in array.

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

    Remove these two code and ur PageViewer will stop