5

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

Quango
  • 12,338
  • 6
  • 48
  • 83
Luke
  • 51
  • 1
  • 3
  • I have a similar question - I'd like to know how to pass data from the PageViewController down to the children – A. Vin May 10 '16 at 17:22
  • Luke, I found a solution for a similar issue I'm having that may work for you - will post as an answer. – A. Vin May 10 '16 at 17:38
  • I really give up to try do it using protocols or notificationcenter, Im just using a UserDefaults, and work well – Daniel Beltrami Jan 05 '19 at 13:13

3 Answers3

7

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.

A. Vin
  • 865
  • 1
  • 6
  • 19
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
Marius Fanu
  • 6,589
  • 1
  • 17
  • 19
  • These methods are really helpful. we can get the previous and next viewController and we can just call their methods and pass data through those methods after typecasting – Muhammad Adil Mar 30 '17 at 19:39
-1

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
    }
Mahesh Giri
  • 1,810
  • 19
  • 27