HideCode HideCode - 1 year ago 309
Swift Question

value of type 'UIViewController' has no member 'addSubview' Swift2

I have this error:

value of type 'UIViewController' has no member 'addSubview'

in this code. Do you know why?

override func viewDidLoad() {
self.currentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ComponentA")
self.currentViewController!.view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.currentViewController?.view, toView: self.containerView)

Answer Source

self is an instance of UIViewController. It defines methods like viewDidLoad(), addChildViewController(), etc.

The method addSubview() is instead defined on the class UIView. If the view controller has a view set for its view property, you can access it with the call self.view.addSubview().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download