How do you instantiate a Storyboard from a file within an iOS playground?

亡梦爱人 提交于 2019-11-29 07:19:23

问题


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:

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..."

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


回答1:


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:

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/

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

Hope it helps




回答2:


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):

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:

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:

Playground Example Projects



来源:https://stackoverflow.com/questions/28176123/how-do-you-instantiate-a-storyboard-from-a-file-within-an-ios-playground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!