ios - Swift:从 xib 以编程方式创建 UIViewController

标签 ios objective-c swift uiviewcontroller

这几天我正在接近 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我不明白哪里出了问题!

enter image description here

最佳答案

对不起,我从 here 得到了答案,你错过了类似的东西(你在 xib 中没有 View 所以添加一个然后执行此操作):

enter image description here

如果您使用的是 Xib,请按照此操作

按照以下步骤

1) open your xib file then right click on files owner and drag to your first view

enter image description here

2) then bind that view with outlet of "view"

enter image description here

希望你能得到它...

关于ios - Swift:从 xib 以编程方式创建 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255183/

相关文章:

ios - Tableview 的委托(delegate)方法未调用

iOS : using activity indicator in loading for second view controller

xcode - 失败的初始化程序'init(fileURL :) cannot override a non-failable initializer

ios - 在 iOS 中出现键盘时启用滚动内容

ios - (Swift) 在我的位置 MapKit 内设置一个 1 公里的半径

ios - sendMessage 的问题 - WatchConnectivity

ios - 应用程序图标未显示在 CallKit UI 中

android - npm WARN react-native-maps@0.14.0 需要 react@>=15.4.0 的对等体,但没有安装

objective-c - AVAudioPlayer audioPlayerDidFinishPlaying :successfully: not called

CIHistogramDisplayFilter 上的 Objective-C 到 Swift 转换问题