Swift iOS: how to trigger next page using buttons

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

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

6条回答
  •  情深已故
    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.

提交回复
热议问题