I have three View Controllers (VC1, Parent View Controller and Child View Controller). How do I pass data from the VC1 to the child View controller when the ParentVC is loaded? Normally I would be able to use this in the First View Controller
var text = "Hello"
var sVC = SecondViewController()
sVC.string = text
and it would pass Hello
to the variable string
in the second view controller. and then do the same thing to pass data from the Second View Controller to the third one. But unfortunately, this logic is not working. I've tried to pass the the data from VC1 to the ParentVC then to the ChildVC. I've also tried to pass the data from VC1 directly to the ChildVC but that does not seem to work either.
Here is what I have
import UIKit
class ViewController1: UIViewController {
var a = "Test"
override func viewDidLoad() {
var pVC = ParentViewController()
pVC.a = a
}
}
I'm able to pass data to the Parent View Controller and it prints Test
but it does not for the Child View Controller
import UIKit
class ParentViewController: UIViewController {
var pageMenu: CAPSPageMenu?
var a = ""
func test() {
var cvC = ChildViewController(frame: self.view.frame)
cvC.a = a
}
override func viewDidLoad() {
println(a)
}
override func viewWillAppear(animated: Bool) {
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// MARK: - Scroll menu setup
// Initialize view controllers to display and place in array
var controllerArray : [UIViewController] = []
var controller1 : ChildViewController = ChildViewController(nibName: nil, bundle: nil)
controller1.title = "View Controller"
controllerArray.append(controller1)
// Initialize scroll menu
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 65, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)
self.view.addSubview(pageMenu!.view)
}
}
And in the Child View Controller I have:
import UIKit
class ChildViewController: UIViewController {
var a = ""
convenience init(frame:CGRect){
self.init(nibName: nil, bundle: nil)
self.view.frame = frame
}
override func viewDidLoad() {
var pVC = ParentViewController()
pVC.test()
println(a)
}