How to pass data from child to parent view controller? in swift

I usually use closures for this purpose. Much simpler and less verbose than delegates: class MainViewController: UIViewController { func showChildViewController() { guard let vc = storyboard?.instantiateViewControllerWithIdentifier(“ChildViewController”) as? ChildViewController else { return } vc.didSelectItem = { [weak self](item) in if let vc = self { // Do something with the item. } } presentViewController(vc, animated: true, … Read more

Add a child view controller’s view to a subview of the parent view controller

It doesn’t really matter which view you are adding the child viewController to. If a view of a viewController is added to another viewController you need set it properly. tableViewController.view.frame = self.contentView.bounds; [self.contentView addSubview:tableViewController.view]; /*Calling the addChildViewController: method also calls the child’s willMoveToParentViewController: method automatically */ [self addChildViewController:tableViewController]; [tableViewController didMoveToParentViewController:self]; Source code

How to add a Container View programmatically

A storyboard “container view” is just a standard UIView object. There is no special “container view” type. In fact, if you look at the view hierarchy, you can see that the “container view” is a standard UIView: To achieve this programmatically, you employ “view controller containment”: Instantiate the child view controller by calling instantiateViewController(withIdentifier:) on … Read more