如何从iOS操场中的文件实例化故事板?

时间:2023-01-23 22:23:57

Let's assume you've copied a Main.storyboard from an Xcode 6 project into a standalone playground's Resources directory. How can you instantiate a UIStoryboard using the Main.storyboard file? Trying to use the default via nil doesn't work:

假设您已将Xcode 6项目中的Main.storyboard复制到独立的playground的Resources目录中。如何使用Main.storyboard文件实例化UIStoryboard?尝试使用默认值为nil不起作用:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

Nor does explicitly using the main bundle:

也没有明确使用主捆绑:

let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())

Even if the playground is part of the storyboard's Xcode project, I receive the same error: "Could not find a storyboard named 'main' in bundle NSBundle..."

即使操场是故事板的Xcode项目的一部分,我也会收到同样的错误:“找不到捆绑NSBundle中名为'main'的故事板......”

Seems that the bundle path is correct and should be able to deserialize the storyboard file.

似乎捆绑路径是正确的,并且应该能够反序列化故事板文件。

2 个解决方案

#1


11  

I found this:

我找到了这个:

"You can throw a xib file into your Resources, but the playground cannot read it or run it because it’s not in compiled form. Today, I (finallly!) thought of using ibtool to pre-compile my MainMenu.xib file into nib and then load that. When you install Xcode’s command line tools, ibtool gets added to /usr/bin. So all you need to do to compile your nib is issue the following command:

“你可以将一个xib文件放入你的资源中,但操场无法读取或运行它,因为它不是编译形式。今天,我(最终!)想到使用ibtool将我的MainMenu.xib文件预编译成nib和然后加载它。当您安装Xcode的命令行工具时,ibtool会被添加到/ usr / bin。所以编译你的nib所需要做的就是发出以下命令:

ibtool --compile MainMenu.nib MainMenu.xib`

Throw that resulting nib into your playground’s resources folder and you’re ready to load it up."

将生成的笔尖扔进游乐场的资源文件夹,然后就可以加载了它。“

at http://ericasadun.com/2015/03/25/swift-todays-wow-moment-adding-menus-to-playgrounds/

在http://ericasadun.com/2015/03/25/swift-todays-wow-moment-adding-menus-to-playgrounds/

Your storyboard will be compiled into multiple nibs (one per scene)

您的故事板将编译成多个笔尖(每个场景一个)

Hope it helps

希望能帮助到你

#2


3  

Similar to @ybakos answer, but for accessing a framework with a storyboard you want to load (then both the Playground and your app can use the same storyboard directly):

与@ybakos答案类似,但是为了访问您想要加载的故事板的框架(然后Playground和您的应用程序可以直接使用相同的故事板):

If you have a Storyboard in a framework that has been compiled for the simulator and is also embedded in a working project that furthermore lives in a workspace, you will be able to load the framework in a playground in that same workspace via the framework bundle, like so:

如果在为模拟器编译的框架中有一个Storyboard,并且它还嵌入到工作区中的工作项目中,那么您将能够通过框架包在同一工作区的操场中加载框架,像这样:

let frameworkBundle = Bundle(identifier: "com.kigisoft.KGWeatherMap")
let storyboard = UIStoryboard(name: "WeatherMap", bundle: frameworkBundle)

You can download a sample project "StoryboardPlay" from this Dropbox link:

您可以从此Dropbox链接下载示例项目“StoryboardPlay”:

Playground Example Projects

游乐场示例项目

#1


11  

I found this:

我找到了这个:

"You can throw a xib file into your Resources, but the playground cannot read it or run it because it’s not in compiled form. Today, I (finallly!) thought of using ibtool to pre-compile my MainMenu.xib file into nib and then load that. When you install Xcode’s command line tools, ibtool gets added to /usr/bin. So all you need to do to compile your nib is issue the following command:

“你可以将一个xib文件放入你的资源中,但操场无法读取或运行它,因为它不是编译形式。今天,我(最终!)想到使用ibtool将我的MainMenu.xib文件预编译成nib和然后加载它。当您安装Xcode的命令行工具时,ibtool会被添加到/ usr / bin。所以编译你的nib所需要做的就是发出以下命令:

ibtool --compile MainMenu.nib MainMenu.xib`

Throw that resulting nib into your playground’s resources folder and you’re ready to load it up."

将生成的笔尖扔进游乐场的资源文件夹,然后就可以加载了它。“

at http://ericasadun.com/2015/03/25/swift-todays-wow-moment-adding-menus-to-playgrounds/

在http://ericasadun.com/2015/03/25/swift-todays-wow-moment-adding-menus-to-playgrounds/

Your storyboard will be compiled into multiple nibs (one per scene)

您的故事板将编译成多个笔尖(每个场景一个)

Hope it helps

希望能帮助到你

#2


3  

Similar to @ybakos answer, but for accessing a framework with a storyboard you want to load (then both the Playground and your app can use the same storyboard directly):

与@ybakos答案类似,但是为了访问您想要加载的故事板的框架(然后Playground和您的应用程序可以直接使用相同的故事板):

If you have a Storyboard in a framework that has been compiled for the simulator and is also embedded in a working project that furthermore lives in a workspace, you will be able to load the framework in a playground in that same workspace via the framework bundle, like so:

如果在为模拟器编译的框架中有一个Storyboard,并且它还嵌入到工作区中的工作项目中,那么您将能够通过框架包在同一工作区的操场中加载框架,像这样:

let frameworkBundle = Bundle(identifier: "com.kigisoft.KGWeatherMap")
let storyboard = UIStoryboard(name: "WeatherMap", bundle: frameworkBundle)

You can download a sample project "StoryboardPlay" from this Dropbox link:

您可以从此Dropbox链接下载示例项目“StoryboardPlay”:

Playground Example Projects

游乐场示例项目