Swift:从xib编程创建UIViewController

22 浏览
0 Comments

Swift:从xib编程创建UIViewController

这些天我正在学习Swift,我有一个问题。

如果我想要以编程方式创建一个新的UIViewController该怎么办呢?

对于一个空/新的视图控制器很容易:

var controller: UIViewController = UIViewController()
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)

但是,我有一个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.'

我已经阅读了这篇帖子,但我不明白问题出在哪里!

\"enter

admin 更改状态以发布 2023年5月23日
0
0 Comments

很抱歉我从这里获取了答案,你可能忘了以下类似的东西(在xib中没有视图,因此需要添加一个,然后做这个):

enter image description here

如果你正在使用Xib,则按照以下步骤操作

请遵循以下步骤

1)打开你的xib文件,然后右键单击文件拥有者并将其拖动到你的第一个视图上

enter image description here

2)然后将该视图绑定到"view"的出口

enter image description here

希望你能理解...

0