Passing data between UIPageViewController Child vi

2020-07-14 06:05发布

问题:

I cant seem to find a specific answer to my direct dilemma.

I have a UIPageViewController that programmatically loads through 6 child UIView scenes. They host various stages of an 'add' element functionality. Currently the PageViewController Class adds each child view into an array and instantiates them when required with:

storyboard?.instantiateViewControllerWithIdentifier("editViewController")

The natural delegate is set-up to swipe between each child scene and therefore no "prepareForSegue" function is run.

As the pages swipe, a new scene with different identifier is instantiated.

From my reading, I am now attempting to set-up a delegate that takes an instance of a Dictionary, that reads/stores input from each stage (different child UIPageView scenes) of the process and updates the Dictionary.

I am using the "viewWillDissapear" and "viewWillAppear" methods to help set-up the passing data into the delegate.

Unfortunately I am running into problems, and due to my lack of experience, and not much on this specific problem, I'm needing help!

Main question:

Can I set-up a data delegate in the UIPageViewController class, that 'talks' or can be accessed by the child UIViews?

OR

Does anyone know of a good solution to pass my Dictionary from child to child to child????

回答1:

Instead of passing data from child->child, you could pass it from child->parent->child.

First, when you instantiate each child in the PageViewController with storyboard?.instantiateViewControllerWithIdentifier("editViewController"), save a reference to it.

So something like

var firstViewController: EditViewController?
...
firstViewController = storyboard?.instantiateViewControllerWithIdentifier("editViewController") as EditViewController

Next, get a reference in each child ViewController to the PageViewController using self.parentViewController.

var myPageViewController: PageViewContrller (or whatever yours is called) 
....
myPageViewController = self.parentViewController() as PageViewController (or whatever yours is called)

Now, using those two references, you can call functions in the PageViewController and in the children to pass data back and forth.



回答2:

You should look over the UIPageViewController delegate here.

You get these 2 methods that gets called before and after the transition between the viewcontrollers.

- pageViewController:willTransitionToViewControllers: // called before the transition starts
- pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:  // called after the transition is finished


回答3:

It is very easy to send data from PageViewController to its childs without using any segues

override func viewDidLoad() {
    super.viewDidLoad()
      self.delegate=self
      self.dataSource=self
    let childViewController=storyboard?.instantiateViewController(withIdentifier: "some identifier of view") as! ChildViewController
    if let cid = challengeId{
        print("Page Challenge id \(cid)")
    childViewController.challengeId=cid
    }