Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading ViewController from xib file

I had a MyViewController.swift and a MyViewController.xib presenting the layout of MyViewController.

I tried different methods to load this view controller including:

//1 let myVC = UINib(nibName: "MyViewController", bundle:        nil).instantiateWithOwner(nil, options: nil)[0] as? MyViewController  //2 let myVC = NSBundle.mainBundle().loadNibNamed("MyViewController", owner: self, options: nil)[0] as? MyViewController  //3 let myVC = MyViewController(nibName: "MyViewController", bundle: nil) 

The third one is the only successful initialisation, but the previous two are causing error:

Terminating app due to uncaught exception 'NSUnknownKeyException',

reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.

What's wrong with those loading methods?

like image 490
bluenowhere Avatar asked May 05 '16 09:05

bluenowhere


1 Answers

Swift 3

let myViewController = MyViewController(nibName: "MyViewController", bundle: nil) self.present(myViewController, animated: true, completion: nil) 

or push in navigation controller

self.navigationController!.pushViewController(MyViewController(nibName: "MyViewController", bundle: nil), animated: true) 
like image 119
Marcos Reboucas Avatar answered Oct 03 '22 17:10

Marcos Reboucas