Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data from View Controller to Child Controller in Swift

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)
}  
like image 445
Pablo Picasso Avatar asked Jul 08 '15 05:07

Pablo Picasso


1 Answers

When loading the ParentViewController from your ViewController, there are no views loaded into memory. It's just an instance of your ParentViewController class.

override func viewDidLoad() {
   var pVC = ParentViewController()
   pVC.a = a
}

And i guess you are loading or presenting this controller somewhere else in your code. If so, it makes sense that you're successfully printing the variable being set.

println(a) -> "Test"

However, when you are instantiating your ChildViewController, you are providing a frame for its convenience init and you are actually initiating the view hierarchy, which in turn fires the viewDidLoad event. This is executed before the a variable is being set from ParentViewController -> ViewDidLoad. And your result from printing the a variable is actually an empty string.

One option is to put your logic within their own functions that you explicitly call. Optionally with your data as parameters or you set the variables before calling the function(s).

You should check out some of these to get a better understanding of how things work and paths you can follow to accomplish what you want.

Instantiate and Present a viewController in Swift

UIViewController class reference

How can I passing data from parent view controller to child view controller?

Hope this helps!

like image 99
Laffen Avatar answered Nov 05 '22 20:11

Laffen