Pass Data from View Controller to Child Controller in Swift

Swift - Pass data from parent view controller to child view controller

When you add a Container View in storyboard it adds a child view controller and a segue from the parent view controller to child view controller.

Sample Image

This segue is performed immediately after the parent view controller is loaded. Pass the data from EventDetails to InvitedController in prepare for segue method

class EventsMainController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "EventDetails" {
if let eventDetails = segue.destination as? EventDetails {
eventDetails.eventId = "FromEventsMainController"
}
}
}
}
class EventDetails: UIViewController {
var eventId = String()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "InvitedController" {
if let invitedController = segue.destination as? InvitedController {
invitedController.eventId = self.eventId
}
}
}
}
class InvitedController: UIViewController {
var eventId = String()
override func viewDidLoad() {
super.viewDidLoad()
print(eventId)//FromEventsMainController
}
}

Pass data from View Controller to Child Controller in Swift

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!

How to pass data from parent view controller to child container view controller

You can try it more early like

let childVC = storyboard!.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC
childVC.boolVariable = true

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

Option 1

If you're passing from a parent to a child view controller, then just set the property of your childViewController from the parent.

customChildViewController.someRandomProperty = @"some random value";
[self presentViewController:customChildViewController animated:YES completion:nil];

Option 2

Or, you can set up a delegate

Step 1: set up protocol above interface in ChildViewController.h file

@protocol ChildViewControllerDelegate

- (NSDictionary *) giveMeData;

@end

@interface .....

Step 2: create delegate property in ChildViewController.h interface

@property (strong, nonatomic) id<ChildViewControllerDelegate>delegate

Step 3: declare delegate protocol in ParentViewController.h

@interface ParentViewController : UIViewController <ChildViewControllerDelegate>

Step 4: in ParentViewController.m add this method:

- (NSDictionary *) giveMeData {
NSMutableDictionary * dataToReturn = [[NSMutableDictionary alloc]init];
dataToReturn[@"some"] = @"random";
dataToReturn[@"data"] = @"here";
return dataToReturn;
}

Step 5: before launching childViewController declare delegate property.

childViewController.delegate = self;
[self presentViewController:childViewController animated:YES completion:nil];

Step 6: in child view controller whenever you want data add this

NSMutableDictionary * dataFromParent = [self.delegate giveMeData];

The parentVC will run 'giveMeData' and return a NSMutableDictionary (adjust for whatever data you want)

How can I pass data from a parent view controller to an embedded view controller in Swift?

A way to achiеve this is to get the child view controller instance in the parent's viewDidLoad. It appears that the parent's viewDidLoad: gets called after the child's viewDidLoad:, which means the label is already created in the child's view.

override func viewDidLoad() {
super.viewDidLoad()

if let childVC = self.childViewControllers.first as? ChildVC {
childVC.someLabel.text = "I'm here. Aye-aye."
}
}


Related Topics



Leave a reply



Submit