Carabinieri
Carabinieri

Reputation: 1

How could I pass data to childView

About

I'm developping ios app with MXParallaxHeader. I use it in order to create twitter-like UI. However, I don't know how to pass data from MXScrollView to childViewController when I go MXScrollView page.

Question

How could I pass data from MXScrollView to childViewController?

Sample Code

PreviousViewController

//MXScrollViewController=ParentVC of MarkerInfoTabViewController
class PreviousViewController:UIViewController {
.
.
    @IBAction func goMXScrollView(_ sender: Any) {
        let nextView = self.storyboard?.instantiateViewController(withIdentifier: "MXScrollViewController") as! MXScrollViewController
        self.navigationController?.pushViewController(nextView, animated: true)
    }
}

childViewController

//MarkerInfoTabViewController(=childVC of MXScrollView)

import Tabman
import UIKit
import MXParallaxHeader

class MarkerInfoTabViewController: TabmanViewController {

    var marker:Marker!
    private var viewControllers = [UIViewController?]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let leftView = self.storyboard?.instantiateViewController(withIdentifier: "MarkerInfoLeftViewController") as! MarkerInfoLeftViewController
        let rightView = self.storyboard?.instantiateViewController(withIdentifier: "MarkerInfoRightViewController") as! MarkerInfoRightViewController
        
        //↓I want to pass data here
        leftView.marker = marker

        viewControllers = [leftView, rightView]
        
        self.dataSource = self

        // Create bar
        let bar = TMBar.ButtonBar()
        bar.layout.transitionStyle = .snap // Customize

        // Add to view
        addBar(bar, dataSource: self, at: .top)
    }
}

My App Image

storyboard

segue to header

segue to childVC

Reference

My app is mostly the same as this app.

Upvotes: 0

Views: 79

Answers (1)

Roman Ryzhiy
Roman Ryzhiy

Reputation: 1636

Just define a property in the child controller class

class ChildViewController {
    var passedData: MY_DATA_TYPE
    ...

and set it after the instantiation

let nextView = self.storyboard?.instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
nextView.passedData = MY_PASSED_DATA
self.navigationController?.pushViewController(nextView, animated: true)

Upvotes: 2

Related Questions