Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 646
ybakos Avatar asked Jan 27 '15 17:01

ybakos


People also ask

How do I use Xcode playgrounds?

To open Playground on Xcode, navigate to File in the menu and click on New > Playground... To test our square function, we will choose the Blank template. Name your Playground file, then click on Create.

How do I create a nib File in Xcode?

Create a XIB FileRight click on the portion of the screen where your project's files are (view controller, storyboard, etc), and choose new file . Xcode will prompt you for which file type you'd like to create. Choose the View option under the user interface menu.

Can you code in Swift playgrounds?

Swift Playgrounds can satisfy your curiosity about what coding is and how it works, but it doesn't really let you write apps. Not even a super-simple basic one. The code you write can't leave the app; it can't even leave that particular puzzle page!


1 Answers

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

like image 50
Martin Lockett Avatar answered Sep 28 '22 02:09

Martin Lockett