UIStoryboard: What is the right way to get an active storyboard?

I am currently furiously digging through all the documents and did not quite find what I was looking for. I suspect this is a real dr! Answer.

I just need to find the active storyboard in the main set, and you want to know how to do it.

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

I know how to kill him by turning on the idiom, but I feel that it is ... kludge.

What is the right way to do this?

UPDATE:

OK I found him.

As usual, when the stack overflows (Apple's official documentation site;).

Here is the code I settled on:

 UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]]; 
+48
ios runtime storyboard
Mar 24 '12 at 16:59
source share
4 answers

OK As my comment above shows, I found the answer to (poorly worded question):

I wanted to get the main (not active) storyboard, since I do not use several storyboards for one incarnation. I use the standard storyboard model 1 for iPhone and 1 for iPad. I just need the cleanest way to get the storyboard so I can use it to create a view controller.

I found the answer in this stack overflow message and implemented it with the following code:

 UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]]; 
+26
Mar 25 '12 at 3:17
source share

If you want to get an active storyboard for the viewController, there is a storyboard property. Here's how I solved it, instead of creating a new instance:

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

In Swift, you would call:

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

You can also be much safer using protection from the navigation controller and storyboard. I used as! to ensure that you get a LoginController.

+38
Jun 25 2018-12-12T00:
source share

In Swift, you should use the following syntax:

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

Note that passing nil to bundle will cause the call to automatically reference your main package.

If you are in the view controller that you have on the storyboard, and you want to directly create a storyboard there, you can simply do:

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

Note that in the latter case, self.storyboard will return an optional storyboard ( Storyboard? ), So if you want to use it, unpack it like this:

 if let storyboard = self.storyboard { // access storyboard here } 
+6
Jul 20 '16 at 15:40
source share

I just copied the pasted code form above the updated question so that everyone can see this as an answer.

 UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]]; 
+1
Jun 21 '16 at 9:18
source share



All Articles