Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get a Storyboard ID?

Trying to see if a UIViewController or UIView can identify its Storyboard ID. So was hoping for:

UIViewController *aViewController;
NSString *storyboardID = aViewController.storyboard.id;  //not an actual property

or:

NSString *storyboardID = [aViewController.storyboard valueForKey:@"storyboardId"];  //also not a working call

But no joy and couldn't find a similar solution online. Does anyone know if this is even possible?

like image 466
Jalakoo Avatar asked Dec 04 '12 17:12

Jalakoo


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.

What is a storyboard ID?

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.

How do you give a segue an identifier in a storyboard?

Assigning an identifier for the segue:Select the segue, from the attribute inspector you'll see "Identifier" text field, that's it! make sure to insert the exact same name that used in performSegueWithIdentifier .

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.


4 Answers

You can use the restorationIdentifier, it's right above the Storyboard identifier and it's a UIViewController property.

like image 178
Lolloz89 Avatar answered Oct 15 '22 16:10

Lolloz89


You can use the Restoration ID:

NSString *restorationId = self.restorationIdentifier;

Just check the checkbox 'Use Storyboard ID'

like image 40
Erickson1 Avatar answered Oct 15 '22 14:10

Erickson1


The storyboard id is only meant to find and instantiate a VC from a storyboard. As written in the UIStoryboard reference:

"This identifier is not a property of the view controller object itself and is only used by the storyboard file to locate the view controller."

Why do you need it?

like image 21
LombaX Avatar answered Oct 15 '22 16:10

LombaX


You can also try doing something like this:-

NSString *storyboardId = [viewController valueForKey:@"storyboardIdentifier"];

This will precisely give you the Storyboard Id that you have set via interface builder.

Swift extension:

extension UIViewController {
    var storyboardId: String { 
        return value(forKey: "storyboardIdentifier") as? String
    }
}
like image 12
Apple_iOS0304 Avatar answered Oct 15 '22 15:10

Apple_iOS0304