Swift iOS: how to trigger next page using buttons

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

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

相关标签:
6条回答
  • 2020-12-15 12:07

    Swift 4.2 Very easy way to move on your desire View Controller

    //MARK:- Programatically move to Next Page or Previous Page
    //Your Desire index in Your UIViewController Array
        let arr : [UIViewController] = [arrvc[1]] 
        pagecontroller.setViewControllers(arr,
                                        direction: UIPageViewController.NavigationDirection.reverse,
                                        animated: true,
                                        completion: nil)
    
    0 讨论(0)
  • 2020-12-15 12:12

    You can use this function:

    func movePageToIndex(pageIndex NSInteger)->() {
        var viewControllers = []
        var direction : UIPageViewControllerNavigationDirection! 
        if (pageIndex < currentIndex) {
            direction = UIPageViewControllerNavigationDirection.Reverse
        } else {
            direction = UIPageViewControllerNavigationDirection.Forward
        }
        viewControllers = @[[self.getItemController(pageIndex)]]
    
        WEAKSELF weakSelf = self;
        self.pageViewController.setViewControllers:viewControllers direction:direction animated:false completion:nil];
    }
    
    0 讨论(0)
  • 2020-12-15 12:17

    You can use setViewControllers:direction:animated:completion: to turn your pages programatically.

    Just pass along the previous or next page's view controller and you should be all set.

    0 讨论(0)
  • 2020-12-15 12:17

    You can use:

    For next page

    pageViewController.setViewControllers(startingViewControllers,
                                                    direction: UIPageViewControllerNavigationDirection.Forward,
                                                    animated: true,
                                                    completion: nil)
    

    For previous page

    pageViewController.setViewControllers(startingViewControllers,
                                                    direction: UIPageViewControllerNavigationDirection.Reverse,
                                                    animated: true,
                                                    completion: nil)
    
    0 讨论(0)
  • 2020-12-15 12:21

    Michael Dautermann's answer is perfectly correct, as this screencast shows:

    What you're seeing is a page view controller with multiple pages (numbered so you can see the order), each page containing a Next button, and I'm repeatedly pressing the Next button to navigate to the next page.

    Like yours, my project, illustrated in the screencast above, has a view controller hierarchy:

    • UITabBarController

    • ViewController

    • UIPageViewController

    • Page (which has a main view, and the label and buttons are subviews of that)

    It appears that the heart of your question is not so much what method causes a page view controller to navigate to its next or previous page — that, as you have already been told, is simply setViewControllers:... — but how the button communicates up the view controller hierarchy. In my example, that means sending a message from the button inside Page's view, past the Page view controller, past the UIPageViewController, and up to the ViewController, which then tells th UIPageViewController what to do.

    I can think of numerous ways to do that:

    • The button posts a notification for which the ViewController is registered

    • The button sends a nil-targeted action for which the ViewController has a handler

    • The button sends a message to the tab bar controller (its tabBarController property), which then sends a message down to its currently selected view controller, the ViewController

    • The button sends a message to its view controller (configured in the nib or storyboard as an action), which sends a message to its parentViewController!.parentViewController!, which is the ViewController.

    Which do I prefer? Personally, I like the nil-targeted action best, because it requires no extra code. The only func pageNextButton() implementation is in ViewController. That is beauty of a nil-targeted action: it walks up the responder chain, looking for a recipient, automatically. The Page view controller and the UIPageViewController have no code at all in this regard.

    I like this much better than parentViewController!.parentViewController!, because the latter requires ultimately that the Page view controller knows the name of a method in the ViewController that it can call, and it must cast down to a ViewController — which is not very portable and gives the Page view controller too much knowledge about its environment, in my opinion. With a nil-targeted action, on the other hand, the sender is totally agnostic about who the actual target will turn out to be! So I like nil-target action best, and notification second best.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题