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
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.
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
}
}
How to pass data to parent view controller
One strategy would be to set the parent as a delegate of the child. You would create this relationship in a prepare(for:sender:)
function.
If you are unfamiliar with delegation, this article is a good guide: https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef
What is the better approach to communicate between parent and child view controllers?
If you simply want to invoke a method on your child view controllers, you could use:
[[self childViewControllers] makeObjectsPerformSelector:@selector(nameOfSelector:) withObject:objectToSend];
or one of the other methods for passing objects along with your selector.
As @Gianluca Tranchedone suggested, you could use delegates or an observer pattern but it really depends on what you need to do and how your code is structured. Making your parent view controller conform to delegates of your child view controllers would allow you to structure your code in a much cleaner way.
Swift - Pass data from Child View to Parent View
So as per the code you have done with declaration but not initialized the delegate. So go to you ParentView and assign the delegate in viewDidLoad or in the function where you prepare to move on child like:-
toChildView.delegate = self
And try again. :-)
Pass data from Parent UIviewController to a Container (ChildViewController) every time the value is changed
After a deeper digging I was able to find a solution for my own question.
here I am going to post if anyone else needs it in the future
so, first of all, I need it to lunch the ChildContoller from the parent controller and not from the storyboard ( so I deleted the segue between the parent and the child.
create a variable for childController like that:
lazy var firstChildViewController: FirstChildViewController = {
let storynoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storynoard.instantiateViewController(identifier: "firstChild") as! FirstChildViewController
self.addChild(viewController)
self.view.addSubview(viewController.view)
return viewController
}()
same thing for the other one if you have two children
and then in the viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
firstChildViewController.view.isHidden = false
secondChildViewController.view.isHidden = true
}
and then in the FirstChildViewController:
override func viewDidLoad() {
super.viewDidLoad()
if let parent = self.parent as? ParentViewController {
parent.delegate = self
}
}
And the problem is solved
Hope it helps someone
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
Table View Controller Duplicate Itself in iOS Swift
iOS Pass Data Back from Viewcontroller2 to Viewcontroller 1 with Presentmodalsegue
Uitabbar Transition Issue Below iOS 11 Swift
Creating a Rtsp Client for Live Audio and Video Broadcasting in Objective C
Allow Users to Send Messages to Multiple Users Simultaneously in a Messaging App
Why Does It Take Such a Long Time for UI to Be Updated from Background Thread
Why Clusterannotationformemberannotations in Mkmapview Is Not Called
How to Set the Collection View Cell Size Exactly Equal to the Collection View in iOS
Swiftui - Scrollviewreader's Scrollto Does Not Scroll
Keep a View Always on Top (Don't Scroll with Keyboard) in Iqkeyboardmanager
Save Uiimage Array in Nsuserdefaults
Making the Map Zoom to User Location and Annotation (Swift 2)
Swift 4: Timer Crashing - Unrecognized Selector Sent to Instance
Issues Reading Data from Firebase Database