Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a StoryBoard ID and how can I use this?

I am new to IOS developing and recently started in Xcode 4.5. I saw for every viewController that i could set some identity variables including the storyboard ID. What is this, and how can I use it?

enter image description here

I started searching on stackoverflow and couldn't find any explanation for it.

I assumed it's not just some stupid label that I can set to remember my controller right? What does it do?

like image 483
RTB Avatar asked Dec 13 '12 20:12

RTB


People also ask

How do I find the storyboard ID?

In the Storyboard, select the view controller that you want to instantiate in code. Make sure the yellow circle is highlighted, and click on the Identity Inspector. Set the custom class as well as the field called "Storyboard ID". You can use the class name as the Storyboard ID.

How does storyboard connect to code?

Xcode connects your storyboard to Swift code by using a combination of the Identity Inspector, IBOutlets and IBActions. On the right side of your addressbar, there is a button you can click to activate the Assistant Editor. The first thing you might notice, is the lack of screen space for each open file.

How do I find the storyboard ID in Swift?

To get the name of a storyboard, just go to your storyboard file, and don't click on any of the View Controllers. Open up the File Inspector, and on the first tab, at the top it will have your name (ie. "Main. storyboard").

How do I get a storyboard view controller?

Open Main. storyboard and select the Tab Bar Controller Scene. On the right, select the Attribute inspector. You'll find a checkbox named Is Initial View Controller.


1 Answers

The storyboard ID is a String field that you can use to create a new ViewController based on that storyboard ViewController. An example use would be from any ViewController:

//Maybe make a button that when clicked calls this method  - (IBAction)buttonPressed:(id)sender {     MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];     [self presentViewController:vc animated:YES completion:nil]; } 

This will create a MyCustomViewController based on the storyboard ViewController you named "MyViewController" and present it above your current View Controller

And if you are in your app delegate you could use

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"                                                          bundle: nil]; 

Edit: Swift

@IBAction func buttonPressed(sender: AnyObject) {     let vc = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyCustomViewController     presentViewController(vc, animated: true, completion: nil) } 

Edit for Swift >= 3:

@IBAction func buttonPressed(sender: Any) {     let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController     present(vc, animated: true, completion: nil) } 

and

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil) 
like image 84
Eric Avatar answered Oct 14 '22 14:10

Eric