这几天我正在接近 swift,我有一个问题。
如果我必须以编程方式创建一个新的 UIViewController 怎么办?
使用空的/新的 View Controller 很容易:
var controller: UIViewController = UIViewController()
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)
现在,我有一个要加载到 Controller 上的 xib 文件:
var controller: UIViewController = UIViewController(nibName: "FeedDetail", bundle: nil)
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)
这次崩溃是因为:
'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FeedDetail" nib but the view outlet was not set.'
我已经读过这个 post我不明白哪里出了问题!
最佳答案
对不起,我从 here 得到了答案,你错过了类似的东西(你在 xib 中没有 View 所以添加一个然后执行此操作):
如果您使用的是 Xib
,请按照此操作
按照以下步骤
1) open your xib file then right click on files owner and drag to your first view
2) then bind that view with outlet of "view"
希望你能得到它...
关于ios - Swift:从 xib 以编程方式创建 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255183/