Swift iOS: how to trigger next page using buttons

后端 未结 6 1143
醉话见心
醉话见心 2020-12-15 11:56

I have a QuizViewController which extends UIViewController, UIPageControllerDelegate, and a UIPageView

6条回答
  •  旧巷少年郎
    2020-12-15 12:25

    Swift 3.0

    Very simple solution is here

    To change page (UIViewController) from a UIViewController, first get the instance of Parent ViewController (which will be UIPageViewController) and then set its current ViewController

    In my case I have a UIPageViewController named "UserDetailsPVC" and it contains 4 pages (UIViewControllers) which are as follows

      PageOneVC:UIViewController
      PageTwoVC:UIViewController
      PageThreeVC:UIViewController
      PageFourVC:UIViewController
    

    In the UIPageViewController lets define an array of pages

     var pages:[UIViewController] = [
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PageOneVC"),
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PageTwoVC"),
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PageThreeVC"),
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PageFourVC")
    ]
    

    Now to change page of UIPageViewController from any of the UIViewController

        // get parent view controller
        let parentVC = self.parent as! UserDetailsPVC
    
        // change page of PageViewController
        parentVC.setViewControllers([parentVC.pages[1]], direction: .forward, animated: true, completion: nil)
    

提交回复
热议问题