ChatGPT解决这个技术问题 Extra ChatGPT

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?

https://i.stack.imgur.com/Sidrj.png

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?


T
Taiwosam

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)

Let's try that, and how do you get the self.storyboard
self.storyboard can be accessed from any viewcontroller. I will edit my answer now so you can see
And what if needed to access it from my AppDelegate or any other class?
Added another edit showing how to access the storyboard from any file.
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.
T
Taiwosam

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).

https://i.stack.imgur.com/V4Ilo.png

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.


The 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.
I was unable to find the Storyboard ID field, so thanks for the picture. It's in the same position on Xcode 9.