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?
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.
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.
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 .
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.
You can use the restorationIdentifier, it's right above the Storyboard identifier and it's a UIViewController property.
You can use the Restoration ID:
NSString *restorationId = self.restorationIdentifier;
Just check the checkbox 'Use Storyboard ID'
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?
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With