1

I am using Swift. I have 2 ViewControllers a) ParentVC & b) ChildVC. When I instantiate the chidVC, I want to pass some data from Parent to Child. Not sure how. Below is the code

ParentVC

override func viewDidLoad() {
    let controller = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as UIViewController!

    //I want to pass some value - For example: controller.id = 3

    addChildViewController(controller)
    containerView.addSubview(controller.view)
    didMoveToParentViewController(controller)      
}

Within Child VC

 var id: Int?

P.S: I definitely want to use "instantiateViewControllerWithIdentifier". I have seen Passing Data Between View Controllers, but thats in ObjC and I am looking for code in Swift as I don't know how to translate it. Also I don't want to use prepareForSegue.

5
  • Possible duplicate of Passing Data between View Controllers
    – Vakas
    Oct 8, 2015 at 7:46
  • Its not a duplicate..that entire answer is in ObjC. I am looking for Swift version as I don't know how to translate it. You should atleast read the question (i specifically mentioned Swift in the title) before downvoting
    – Anuj Arora
    Oct 8, 2015 at 7:54
  • Did you even read that? The link contains answer in Swift as well.
    – Vakas
    Oct 8, 2015 at 7:59
  • 1
    Yes I did..the only swift explaination is for prepareforsegue method..I know how to do that..I dont know how to pass data between Parent & Child when I instantiate. I am not using prepareforsegue because there is no segue happening. If you know how..atleast type in the code..
    – Anuj Arora
    Oct 8, 2015 at 8:03
  • 1
    I don't mind you downviting but atleast post a decent answer -- otherwise pls remove the downvote as noone else will also answer
    – Anuj Arora
    Oct 8, 2015 at 8:18

2 Answers 2

3
  • define yourID in child view controller.
  • pass the ID after instantiating

    let controller = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as YourViewController!
    
    controller.yourID = 100
    
    presentViewController(controller, animated:true, completion:nil)
    
4
  • i tried that..its not working..the error is that UIViewController does not have a member "yourID"...but as I mentioned in the question as well..I have declared the id variable (just as you mentioned as well).
    – Anuj Arora
    Oct 8, 2015 at 8:45
  • your ID is your property you define in Child View Controller. - Open ChildViewController.swift, define var yourID = 0 - Back to parent viewController and pass ID to your child. PS: prevent define id in your code.
    – truongky
    Oct 8, 2015 at 9:15
  • I have declared id in Childview controller class..var id :Int?... in the ParentVC.. let controller = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as UIViewController! controller.id = 100 i get an error that controller does not have a member "id" in the above code if I replace "as UIViewContoller!" as Tplet suggested with "as ChildViewController!" then I get error : expression resolves to an unused i-value really can't figure this out :(
    – Anuj Arora
    Oct 8, 2015 at 9:52
  • let controller = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as UIViewController! Sorry about my mistake. Change UIViewController to ParentVC. Hope this help.
    – truongky
    Oct 26, 2015 at 5:38
1

You almost had it. In the instantiation of your ChildViewController you need cast into the class of your ChildViewController not UIViewController, because UIViewController does not have the property.

so replace

let controller = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as UIViewController!

by

let controller = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as YourChildViewControllerClass!

Edit:
Now you can set the property like truongky answered:

controller.yourId = 100
1
  • hi..just tried this...it gives me a different error in the "controller.yourID" line..it says expression resolves to an unused I-value
    – Anuj Arora
    Oct 8, 2015 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.