What is a StoryBoard ID and how can I use this?
Solution 1
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)
Solution 2
To add to Eric's answer and update it for Xcode 8 and Swift 3:
A storyboard ID does exactly what the name implies: it identifies. Just that it identifies a view controller in a storyboard file. It is how the storyboard knows which view controller is which.
Now, don't be confused by the name. A storyboard ID doesn't identify a 'storyboard'. A storyboard, according to Apple's documentation, 'represents the view controllers for all or part of your app’s user interface.' So, when you have something like the picture below, you have a storyboard called Main.storyboard which has two view controllers, each of which could be given a storyboard ID (their ID in the storyboard).
You can use a view controller's storyboard ID to instantiate and return that view controller. You can then go ahead to manipulate and present it however you want. To use Eric's example, say you want to present a view controller with identifier 'MyViewController' when a button is pressed, you would do it this way:
@IBAction func buttonPressed(sender: Any) {
// Here is where we create an instance of our view controller. instantiateViewController(withIdentifier:) will create an instance of the view controller every time it is called. That means you could create another instance when another button is pressed, for example.
let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController
present(vc, animated: true, completion: nil)
}
Please take note of changes in syntax.
Related videos on Youtube
Comments
-
RTB over 3 years
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?
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?
-
RTB over 11 yearsLet's try that, and how do you get the
self.storyboard
-
Eric over 11 yearsself.storyboard can be accessed from any viewcontroller. I will edit my answer now so you can see
-
RTB over 11 yearsAnd what if needed to access it from my AppDelegate or any other class?
-
Eric over 11 yearsAdded another edit showing how to access the storyboard from any file.
-
rob mayoff over 11 years
self.storyboard
can be accessed from any view controller that was loaded from a storyboard. If the view controller wasn't loaded from a storyboard, that property is nil. -
Eric over 11 yearsGood point, I was assuming the whole project was using Storyboards. I already added a way of accessing the storyboard from other files in my solution
-
Android_kalai over 10 years@Eric Yes i got it..Thanks for your suggestions
-
Umit Kaya about 9 yearsTo make it clear for some: MyCustomViewController is the name of controller you have and using its implementation.
-
Taiwosam almost 7 yearsThe yellow warning icon indicates is due to the fact that the second view controller has no entry point and/or ID. This can be addressed by giving it a storyboard ID or connecting it to another view controller in the storyboard. That way, the storyboard knows how to reach and identify it.
-
Isaac Bosca over 6 yearsI was unable to find the Storyboard ID field, so thanks for the picture. It's in the same position on Xcode 9.