Difference between using addChildViewController or just addSubview?

0

I created a viewController in the storyboard. This viewController puts it inside another main one with self.view.addSubview and I show it in an x, y coordinate. All perfect but I have seen other cases where addChildViewController is also used

When should I use addChildViewController and when not?

    
asked by Popularfan 29.08.2017 в 10:34
source

1 answer

1

I can answer you is very easy.

addSubview : It is a method to add a View to another View as daughters.

addChildViewController : It is a method to add ViewController to other ViewController as children as the name suggests.

There is a big difference. When you for example instances something like this:

let viewController = UIViewController()

You are calling the method init() but the view is not starting or loadView

Then you are creating a% co_of% empty.

The way to do it correctly is as follows:

let viewController = UIViewController()
parentViewController.addChildViewController(viewController) //Aquí se forma la vista y ya puedes acceder a viewController.view y no es nil
parentViewController.view.addSubview(viewController.view) //Aquí añades la vista
    
answered by 17.10.2017 в 11:34