Search code examples
iosswiftmodel-view-controlleruipageviewcontroller

UIPage ViewController, i want to navigate the other second viewcontroller from non uipage viewcontroller


I need Code for UIpageViewcontroller, which will work like below MVC of Image.

PageViewController Flow

Edit:

When I Click on next button of "Other ViewController", it will call ViewController 2 of UIPageViewcontroller. The exact flow is shown in above image.


Solution

  • ok this is one way to do it

    first you need to create a UIPageViewController in the storyboard and set the class to same of this code

    in a new swift file copy and paste this

    class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    //make sure the storyboard PageviewControllers class is set to this class or it wont work
    
    lazy var VCArray: [UIViewController] = {
        //here is where your viewController pages go. in storyboard create multiple viewControllers and give them a storyboard ID here is where the storyboard IDs will be put in
        return [self.VCInstance(name: "firstVC"),
                self.VCInstance(name: "secondVC"),
                self.VCInstance(name: "thirdVC")]
    }()
    
    private func VCInstance(name: String) -> UIViewController {
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.delegate = self
        self.dataSource = self
    
        if let firstVC = VCArray.first {
            setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
        }
    
    
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        for view in self.view.subviews {
            if view is UIScrollView {
                view.frame = UIScreen.main.bounds
            } else if view is UIPageControl {
                view.backgroundColor = UIColor.clear
            }
        }
    }
    
    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = VCArray.index(of: viewController) else {
            return nil
        }
    
        let previousIndex = viewControllerIndex - 1
    
        guard previousIndex >= 0 else {
            return VCArray.last
        }
    
        guard VCArray.count > previousIndex else {
            return nil
        }
        return VCArray[previousIndex]
    }
    
    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = VCArray.index(of: viewController) else {
            return nil
        }
    
        let nextIndex = viewControllerIndex + 1
    
        guard nextIndex < VCArray.count else {
            return VCArray.first
        }
    
        guard VCArray.count > nextIndex else {
            return nil
        }
        return VCArray[nextIndex]
    }
    //this is optional for the page counter at the bottom
    public func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return VCArray.count
    }
    //this is optional for the page counter at the bottom
    public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = VCArray.index(of: firstViewController) else {
            return 0
        }
        return firstViewControllerIndex
    }
    

    }

    Explanation-

    the code above is very customizable you can change whether or not you have a page counter at the bottom or how many pages are displayed by using this it turns multiple storyboard viewControllers into a single-paged viewController and because the pages are individual storyboard viewControllers you can put whatever you want on the pages....if you are still confused check out this video on YouTube