UIStoryboard: What's the Correct Way to Get the Active Storyboard?

IosRuntimeStoryboard

Ios Problem Overview


I am currently furiously digging through all the docs, and haven't quite found what I'm looking for. I suspect it is a real d'oh! answer.

I simply need to find the active storyboard in the main bundle, and want to know the best way to do this.

This is so that I can use the [UIStoryboard storyboardWithName:@"XXX" bundle:mainBundle] to extract the running storyboard.

I know how to kludge it by switching on the idiom, but I feel that this is a...kludge.

What's a correct way of doing this?

Ios Solutions


Solution 1 - Ios

In case you want to get the active storyboard for a viewController, there's a storyboard property. This is how I solved it, instead of making a new instance:

LoginViewController *vc = [navController.storyboard instantiateViewControllerWithIdentifier:@"firstLaunch"];
[navController presentModalViewController:vc animated:YES];

In Swift you'd call:

let loginViewController = navigationController?.storyboard?.instantiateViewController(withIdentifier: "firstLaunch") as! LoginViewController
navigationController?.present(loginViewController, animated: true, completion: nil)

You could also be a lot safer by using guards against the navigation controller and the storyboard. I've used as! so as to guarantee that you're getting a LoginController.

Solution 2 - Ios

OK. As my comment above indicates, I found the answer to the (badly phrased question):

I wanted to be able to get the main (not active) storyboard, as I'm not using multiple storyboards per incarnation. I'm using the standard model of 1 storyboard for iPhone, and 1 for iPad. I just wanted the cleanest way to get the storyboard, so that I could use it to generate a view controller.

I found the answer in https://stackoverflow.com/questions/9530075/ios-access-app-info-plist-variables-in-code">this post on Stack Overflow, and implemented it with the following code:

UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];

Solution 3 - Ios

In Swift, you'd use the following syntax:

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

Note that passing nil to bundle will make the call refer to your main bundle automatically.

If you're in a view controller that you have on the Storyboard and want to instantiate the Storyboard from there directly, you can just do:

let storyboard: UIStoryboard? = self.storyboard // call this inside a VC that is on the Storyboard

Note that in the last case, self.storyboard will return an optional Storyboard (Storyboard?), so if you'd like to use it unwrap it like so:

if let storyboard = self.storyboard {
  // access storyboard here
}

Solution 4 - Ios

I have just copy pasted the code form above updated question so that everyone can see it as an answer.

UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris MarshallView Question on Stackoverflow
Solution 1 - IosolivaresFView Answer on Stackoverflow
Solution 2 - IosChris MarshallView Answer on Stackoverflow
Solution 3 - IosnburkView Answer on Stackoverflow
Solution 4 - IosImran KhanView Answer on Stackoverflow