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

RTB picture RTB · Dec 13, 2012 · Viewed 82.4k times · Source

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?

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?

Answer

Eric picture Eric · Dec 13, 2012

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)