How do I change the UIPageViewController from WITHIN one of the UIViewControllers that is part of the UIPageViewController?

99封情书 提交于 2019-12-08 01:18:21

问题


I have been trying to figure this out for hours, and I must be misunderstanding how UIPageViewController works.

I have 5 setup screens (separate view controllers that are loaded into the UIPageViewController) that I can swipe right + left to see.

BUT! I want to be able to programmatically show the next view controller in line after they dismiss an alert on one of those view controllers.

And so far, the only solution that I've found that gets close, is this solution here ( https://github.com/jeffaburt/UIPageViewController-Post ) that allows a "next" button, but the button isn't on the view controller that's showing, it's on the root view controller that holds the UIPageViewController.

I have the UIPageViewController set up to show 5 different view controllers, with this code.

private(set) lazy var orderedViewControllers: [UIViewController] = {
    return [self.newColoredViewController("setup1"),
            self.newColoredViewController("setup2"),
            self.newColoredViewController("setup3"),
            self.newColoredViewController("setup4"),
            self.newColoredViewController("setup5")]
}()

Then the newColoredViewController function does this:

private func newColoredViewController(color: String) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil) .
        instantiateViewControllerWithIdentifier("\(color)")
}

and here's my viewDidLoad:

 override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = self



    if let SetupViewController = orderedViewControllers.first {
        setViewControllers([SetupViewController],
                           direction: .Forward,
                           animated: true,
                           completion: nil)
    }


}

I must be missing some fundamental understanding, because I've tried so many different ways to change the page programmatically, and nothing happens.

I've tried doing this, but it does nothing:

setViewControllers([orderedViewControllers[3]],
                           direction: .Forward,
                           animated: true,
                           completion: nil)

I've tried doing this, but it does nothing:

func scrollToNextViewController() {
    if let visibleViewController = viewControllers?.first,
        let nextViewController = pageViewController(self,
                                                    viewControllerAfterViewController: visibleViewController) {
        scrollToViewController(nextViewController)
    }
}

Please point me in the right direction!! I can research all day and usually can figure it out thanks to previous stackoverflow questions that are already answered, but here I'm stumped.

Thanks in advance!!


回答1:


I figured out an easier way to do it within the structure I'd already set up. In the UIPageViewController, added this function:

func nextPageWithIndex(index: Int)
{
   // let nextWalkthroughVC = newColoredViewController("setup4")
    let nextWalkthroughVC = orderedViewControllers[index]
        setViewControllers([nextWalkthroughVC], direction: .Forward, animated: true, completion: nil)

}

and then in one of the setup View Controllers I created a button action that ran this code:

@IBAction func yeaaa(sender: AnyObject) {
              let parent = self.parentViewController as! SetupViewController

    parent.nextPageWithIndex(3)
}

(where "SetupViewController" is the UIPageViewController)

YEEHAWWWW that was driving me nuts.

Here's the video that gave me the idea of "self.parentViewController as! SetupViewController" and the "nextPageWithIndex" code: https://www.youtube.com/watch?v=K3CSBxX5VXA




回答2:


I guess in newColoredViewController you create the instances of the view controllers that are to be shown by the pageViewController.

There you shoud pass into a reference to the showing view controller. Something like newSetupViewConroller.setNextDelegate(self).

Define a protocol NextDelegate that has a method performNext

That should be the type of the parameter (and the correlating instance variable) of setNextDelegate. (Alternatively to a setter method you can of course declare some property of type NextDelegate)

Your presenting view controller should conform to this protocol and implement the performNext method.

When you did that then you have access to the presenting view controller (as an object that conforms to the protocol) and you can call its method performNext.

Within the method performNext you are in your presenting view controller and have access to the pageViewController and go from there.

(There are easier ways of doing this, but I think this is a proper one. That is what protocols are made for.)



来源:https://stackoverflow.com/questions/40427327/how-do-i-change-the-uipageviewcontroller-from-within-one-of-the-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!