Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between UIPageViewController Child views with Swift

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????

like image 499
Luke Avatar asked Apr 05 '16 06:04

Luke


2 Answers

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.

like image 51
A. Vin Avatar answered Sep 28 '22 10:09

A. Vin


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
like image 37
Marius Fanu Avatar answered Sep 28 '22 10:09

Marius Fanu