tzef / BmoViewPager

A ViewPager with NavigationBar component based on UIPageViewController and UICollectionView
MIT License
269 stars 33 forks source link

I am unable to get any delegate method or datasource method inside button action. #18

Open AQElementary opened 4 years ago

AQElementary commented 4 years ago

I want to achieve like this:

Have to used Next button which ll move the scroller to other index of controller but unable to do so, Kindly help if there is any thing related this.

Here its what i am doing

import UIKit import BmoViewPager

class ViewController: UIViewController {

@IBOutlet weak var viewPager: BmoViewPager!
@IBOutlet weak var pageControl: UIPageControl!
weak var BmoDelegate: BmoViewPagerDelegate?
weak var BmoDataSource: BmoViewPagerDataSource?

var globalPage = 0
override func viewDidLoad() {
    super.viewDidLoad()

    viewPager.delegate = self
    viewPager.dataSource = self
}

@IBAction func actionNext(_ sender: Any) {

    if self.globalPage < 3 {

        self.globalPage += 1

        BmoDataSource.bmoViewPagerDataSource(_ viewPager: BmoViewPager, viewControllerForPageAt page: Int) -> UIViewController {

            self.globalPage = page

            switch self.globalPage {

            case 0:
                if let vc = storyboard?.instantiateViewController(withIdentifier: "VC1") as? VC1 {
                    return vc
                }
            case 1:
                if let vc = storyboard?.instantiateViewController(withIdentifier: "VC2") as? VC2 {
                    return vc
                }
            case 2:
                if let vc = storyboard?.instantiateViewController(withIdentifier: "VC3") as? VC3 {
                    return vc
                }
            default:
                break
            }
            return UIViewController()
        }

    }
}

}

extension ViewController: BmoViewPagerDataSource { func bmoViewPagerDataSourceNumberOfPage(in viewPager: BmoViewPager) -> Int { return 3 }

func bmoViewPagerDataSource(_ viewPager: BmoViewPager, viewControllerForPageAt page: Int) -> UIViewController {

    self.globalPage = page

    switch self.globalPage {

    case 0:
        if let vc = storyboard?.instantiateViewController(withIdentifier: "VC1") as? VC1 {
            return vc
        }
    case 1:
        if let vc = storyboard?.instantiateViewController(withIdentifier: "VC2") as? VC2 {
            return vc
        }
    case 2:
        if let vc = storyboard?.instantiateViewController(withIdentifier: "VC3") as? VC3 {
            return vc
        }
    default:
        break
    }
    return UIViewController()
}

}

extension ViewController: BmoViewPagerDelegate {

func bmoViewPagerDelegate(_ viewPager: BmoViewPager, pageChanged page: Int) {
    self.globalPage = page
    pageControl.currentPage = self.globalPage
}

}

tzef commented 4 years ago

you can try to set presentedPageIndex of view pager in your action function I think you search presentedPageIndex in the example project, can see some example hope it's helpful to you :)