我有一个MyViewController.swiftMyViewController.xib介绍MyViewController的布局。

我尝试了其他方法来加载此 View Controller ,包括:

//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)

第三个是唯一成功的初始化,但是前两个正在导致错误:



这些加载方法有什么问题?

最佳答案

swift 3

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

或插入导航 Controller
self.navigationController!.pushViewController(MyViewController(nibName: "MyViewController", bundle: nil), animated: true)

关于ios - 从xib文件加载ViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37046573/

10-11 19:48