Passing data between UIPageViewController Child views with Swift

旧时模样 提交于 2019-12-04 08:12:31

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.

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

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