Daniel K.
Daniel K.

Reputation: 1556

How would I reference the main storyboard in my app programmatically in swift?

How would I reference the main storyboard in my app programmatically in swift? I looked into the app delegate for a reference but so far I haven't found one.

Upvotes: 14

Views: 21975

Answers (5)

Alejandro Iván
Alejandro Iván

Reputation: 4051

Even if @ManOfPanda's answer is correct, there are cases where you simply don't have a reference to a UIViewController, so you can grab it from the rootViewController of the UIWindow object from your AppDelegate.

// First import your AppDelegate
import AppDelegate

// ...

// Then get a reference of it.
let appDelegate = UIApplication().shared.delegate as! AppDelegate

// From there, get your UIStoryboard reference from the
// rootViewController in your UIWindow
let rootViewController = appDelegate.window?.rootViewController
let storyboard = rootViewController?.storyboard

You could also, of course, simply create a UIStoryboard by using (as @Mario suggested):

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

But that will, according to Apple documentation, create a new instance of the Storyboard (even if you already have one working). I always prefer to use an existing instance.

init(name:bundle:)

Creates and returns a storyboard object for the specified storyboard resource file.

init(name: String, bundle storyboardBundleOrNil: Bundle?)

Parameters

  • name: The name of the storyboard resource file without the filename extension. This method raises an exception if this parameter is nil.
  • storyboardBundleOrNil: The bundle containing the storyboard file and its related resources. If you specify nil, this method looks in the main bundle of the current application.

Source: Apple documentation

Upvotes: 7

Peter Lapisu
Peter Lapisu

Reputation: 20965

UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

Upvotes: -1

tikhop
tikhop

Reputation: 2087

It's easy. When I've come across a similar problem, I wrote a class that can obtain any resources from main bundle.

//Generate name of the main storyboard file, by default: "Main"
var kMainStoryboardName: String {
    let info = NSBundle.mainBundle().infoDictionary!

    if let value = info["TPMainStoryboardName"] as? String
    {
        return value
    }else{
        return "Main"
    }
}

public class TPBundleResources
{
    class func nib(name: String) -> UINib?
    {
        let nib = UINib(nibName: name, bundle: NSBundle.mainBundle());
        return nib
    }

    //Main storybord
    class func mainStoryboard() -> UIStoryboard
    {
        return storyboard(kMainStoryboardName)
    }

    class func storyboard(name: String) -> UIStoryboard
    {
        let storyboard = UIStoryboard(name: name, bundle: NSBundle.mainBundle())
        return storyboard
    }

    //Obtain file from main bundle by name and fileType
    class func fileFromBundle(fileName: String?, fileType: String?) -> NSURL?
    {
        var url: NSURL?

        if let path = NSBundle.mainBundle().pathForResource(fileName, ofType: fileType)
        {
            url = NSURL.fileURLWithPath(path)
        }

        return url
    }

    class func plistValue(key:String) -> AnyObject?
    {
        let info = NSBundle.mainBundle().infoDictionary!

        if let value: AnyObject = info[key]
        {
            return value
        }else{
            return nil
        }
    }
}


public extension TPBundleResources
{
    //Obtain view controller by name from main storyboard
    class func vcWithName(name: String) -> UIViewController?
    {
        let storyboard = mainStoryboard()
        let viewController: AnyObject! = storyboard.instantiateViewControllerWithIdentifier(name)
        return viewController as? UIViewController
    }

    class func vcWithName(storyboardName:String, name: String) -> UIViewController?
    {
        let sb = storyboard(storyboardName)
        let viewController: AnyObject! = sb.instantiateViewControllerWithIdentifier(name)
        return viewController as? UIViewController
    }

    //Obtain view controller by idx from nib
    class func viewFromNib(nibName: String, atIdx idx:Int) -> UIView?
    {
        let view =  NSBundle.mainBundle().loadNibNamed(nibName, owner: nil, options: nil)[idx] as! UIView
        return view
    }

    class func viewFromNib(nibName: String, owner: AnyObject, atIdx idx:Int) -> UIView?
    {
        let bundle = NSBundle(forClass: owner.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let view = nib.instantiateWithOwner(owner, options: nil)[idx] as? UIView
        return view
    }

    class func viewFromNibV2(nibName: String, owner: AnyObject, atIdx idx:Int) -> UIView?
    {
        let view =  NSBundle.mainBundle().loadNibNamed(nibName, owner: owner, options: nil)[idx] as! UIView
        return view
    }
}

Here are simple examples:

//Get a main storyboard
TPBundleResources.mainStoryboard()

//Get view controller form main storyboard
TPBundleResources.vcWithName("MyViewController")

//Get view from MyView.nib at index 0
TPBundleResources.viewFromNib("MyView", atIdx: 0)

//Get plist value by key
TPBundleResources.plistValue("key")

Upvotes: 8

Mario
Mario

Reputation: 4520

Or any object can get a storyboard by referencing its name and bundle:

let storyboard = UIStoryboard(name: "storyboardNameHere", bundle: nil) //if bundle is nil the main bundle will be used

Upvotes: 13

Daniel K.
Daniel K.

Reputation: 1556

Oh whoops I found the answer...

In another view controller of the that is connected to a storyboard you can simply use:

self.storyboard?

Upvotes: 19

Related Questions