无法在 Swift 的 Storyboard 中加载 UIViewController XIB 文件 [英] Can't Load UIViewController XIB file in Storyboard in Swift

查看:19
本文介绍了无法在 Swift 的 Storyboard 中加载 UIViewController XIB 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读使用 XCode 故事板实例化使用 XIB 进行设计的视图控制器,但我在 Swift 中遇到了麻烦(使用 Xcode 6 Beta 6).我想知道是我做错了什么,还是此功能不再可用?

I've read Using XCode storyboard to instantiate view controller that uses XIB for its design but I'm having troubles making this work in Swift (Using Xcode 6 Beta 6). I'm wondering if I've done something wrong or if this functionality isn't available anymore?

我创建了一个简单的存储库,https://github.com/jer-k/StoryboardTesting-Swift,展示了上述方法.

I created a simple repository, https://github.com/jer-k/StoryboardTesting-Swift, that showcases the above approach.

我设法通过添加和覆盖 init 来解决这个问题

I managed to solve the issue by adding and override to init

required init(coder aDecoder: NSCoder) {
    super.init(nibName: "TestViewController", bundle: NSBundle.mainBundle())
}

但我想知道是否还有可能让故事板为我处理这个问题.用上面的代码创建一个让我的所有 UIViewController 继承的超类并不是世界上最麻烦的事情,但我现在只是好奇.

but I'm wondering if it is still possible to have the storyboard handle this for me. Creating a superclass to have all my UIViewControllers inherit from with the above code isn't the most cumbersome thing in the world, but I'm just curious at this point.

推荐答案

发生的事情是,如果视图控制器是 Swift 类,则 Seed 5 打破了视图控制器可以通过名称找到其 xib 的机制.原因是类的名字,在 Swift 的心目中,和你给它的名字(以及你给 xib 文件的名字)不一样;该名称已被修改",特别是通过在模块名称之前添加(即 Swift 类具有命名空间).

What's happened is that Seed 5 broke the mechanism whereby a view controller can find its xib by name, if the view controller is a Swift class. The reason is that the name of the class, in Swift's mind, is not the same as the name you gave it (and the name you gave the xib file); the name has been "mangled", in particular by prepending the module name (i.e. Swift classes have namespacing).

我提供了三种解决方法:

I offer three workarounds:

  • 您的解决方法很好(按名称显式加载 .xib 文件)

  • Your workaround is a good one (load the .xib file by name explicitly)

将 .xib 文件命名为 MyModule.TestViewController.xib,其中 MyModule 是您的包的名称(即项目的名称)(这是Apple 的建议,但我讨厌它)

Name the .xib file MyModule.TestViewController.xib, where MyModule is the name of your bundle (i.e. the name of the project) (this is what Apple advises, but I hate it)

在视图控制器的 class 声明之前使用 @objc(TestViewController) 来克服破坏机制的名称修改(这是我喜欢的方法))

Use @objc(TestViewController) before the view controller's class declaration to overcome the name mangling which is what's breaking the mechanism (this is the approach I favor)

在此处查看我的讨论:https://stackoverflow.com/a/25163757/341994 和我的进一步讨论链接从那里:https://stackoverflow.com/a/25152545/341994

See my discussion here: https://stackoverflow.com/a/25163757/341994 and my further discussion linked to from there: https://stackoverflow.com/a/25152545/341994

EDIT 此错误已在 iOS 9 beta 4 中修复.如果 nib 文件搜索失败,iOS 9 现在会从视图控制器类名称中剥离模块名称并再次执行 nib 文件搜索.

EDIT This bug is fixed in iOS 9 beta 4. If the nib file search fails, iOS 9 now strips the module name off the view controller class name and performs the nib file search a second time.

这篇关于无法在 Swift 的 Storyboard 中加载 UIViewController XIB 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆