Search code examples
iosswiftuipageviewcontroller

PageviewController selected index to particular viewcontroller


I have pageViewcontroller which is automatically slideing from one page to other page.

When I click on the particular page, I want to go to Another ViewController, I have seen many apps which displaying offer in a slide, when we click the slide index, it will go to the particular page.


Solution

  • Simply add this on your UIPageControl:

    pageControl.addTarget(self, action: #selector(pageChanged(_:)), for: .valueChanged)
    

    Then catch the event:

    @objc func pageChanged(_ sender: UIPageControl) {
        // Go to the appropriate controller
        let controllerToDisplay = UIViewController() // pick the appropriate controller here
        self.setViewControllers([controllerToDisplay], direction: .forward, animated: true, completion: nil)
    }