Search code examples
swiftuipageviewcontroller

Perform Onboarding Pager with a boutton


I'm using an existing project (https://github.com/ThornTechPublic/Onboarding-With-UIPageViewController-Final ) . I want to perform the slide with a boutton also. I want my button "Next" to do the same functionality as swiping so when the user taps it, it moves to the next screen. Button in blue . How to do that ? thanks in advance .


Solution

  • Here is how I do it: I use an NSNotification on the page, that lets the OnboardingPager know when the button is pushed, and do the appropriate action.

    Here's an example: in the StepZero page, I did the following

    @IBAction func nextPressed(sender: UIButton) {
        NSNotificationCenter.defaultCenter().postNotificationName("testbutton", object: nil)
    }
    

    Then in the OnboardingPager class, I added the following

    override func viewDidLoad() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(nextPage(_:)), name: "testbutton", object: nil)
    }
    
    func nextPage(notification:NSNotification) {
        self.setViewControllers([getStepOne()],
                 direction: UIPageViewControllerNavigationDirection.Forward,
                 animated: true,
                 completion: nil)
    }
    

    This lets you go from the first page (StepZero) to the second (StepOne).