ios - 获取 Nib 名称/ Nib 到底是什么?

标签 ios swift nib

我正在尝试使用来自 this pre-built page menu pod from github

在说明中,它说:

    var controller : UIViewController = UIViewController(nibName:"controllerNibName", bundle: nil)
    controller.title = "SAMPLE TITLE"
    controllerArray.append(controller)

这是我的代码:

    var chatAreaCtrl : UIViewController = ChatAreaCtrl(nibName: "ChatAreaCtrl", bundle: nil)
    chatAreaCtrl.title = "Chats"
    controllerArray.append(chatAreaCtrl)

这让我犯了错误:

    Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: ...(loaded)' with name 'ChatAreaCtrl''

我是编程和 Swift 的新手,但我想我没有正确指定 Nib 是什么?

谢谢!

最佳答案

Nibfiles 是用户界面文件的旧名称。它们现在实际上是 .xib 文件(文件扩展名在前一段时间发生了变化。)但是老派开发人员和方法仍然使用术语 nib。

仍然支持 Nib 文件(或 XIB 文件,如果您愿意),但大多数人改用 Storyboard。

如果您在 Xcode 中创建一个用户界面文件并以名称 ChatAreaCtl.xib 保存它,那么您将能够使用您显示的代码对其进行初始化。

与 Storyboard 等效的是在 Storyboard 中创建一个新场景,该场景定义 View Controller 的界面,然后使用 instantiateViewControllerWithIdentifier 创建该 View Controller 的实例(instantiateViewControllerWithIdentifier 是等同于 initWithNibName:bundle: 的 Storyboard。或者我想我应该为 Swift 说 UIViewController(_nibName:bundle:)。仍在使用语法Swift 中的函数模板。)

关于ios - 获取 Nib 名称/ Nib 到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32238166/

相关文章:

cocoa - 使用 NSViewController 加载 Nib 后...如何访问 Nib 中的 NSButton?

ios - UIButton 垂直底部对齐不起作用

ios - Pan 手势识别器 : Location of touch crashes once the pan gesture is released

ios - 本地通知标识符

ios - xCode7.3 上的 Swift 错误消息

ios - Swift 中频繁的后台位置更新

macos - 如何编译 .nib 文件

ios - UITableViewCells Nib 在滚动时显示默认值

ios - 如何在第二个 View Controller 中初始化 View Controller socket ?

ios - 单击按钮快速推送 UIViewController 两次