i\'m 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?
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)
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.