Hobbes
Hobbes

Reputation: 119

How to load a viewController's view to an other viewController's view in Swift?

How to load a viewController's view to an other viewController's view in Swift ?

I have tried :

let myView = NSBundle.mainBundle().loadNibNamed("xibView", owner: nil, options: nil)[0] as UIView

self.view.addSubview(myView)

However, I have received this message :

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7fefaaf9e550> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'

Regards

Upvotes: 2

Views: 94

Answers (1)

Dario
Dario

Reputation: 3135

It seems like you have created a custom view class and linked some view with this class you've created (set as the file owner). But, when you create the class you need to create the File Owner. So, for example, if you set the file owner to "MyCustomView" you have to create it and set it by code in the owner parameter. In your example would be:

let myCustomView = MyCustomView()

let myView = NSBundle.mainBundle().loadNibNamed("xibView", owner: myCustomView, options: nil)[0] as UIView

self.view.addSubview(myView)

Upvotes: 1

Related Questions