Programmatically get a Storyboard ID?

IosObjective CXcodeStoryboardIdentity

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

You can use the Restoration ID:

NSString *restorationId = self.restorationIdentifier;

Just check the checkbox 'Use Storyboard ID'

Solution 3 - Ios

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?

Solution 4 - Ios

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
    }
}

Solution 5 - Ios

The most reliable method for returning the "id" of the UIViewController or UIView is...

NSString *viewControllerName = [[NSString alloc] initWithString:viewController.nibName];

This will return... "29w-Ic-LNo-view-FDu-oq-UpZ", where "29w-Ic-LNo" is the Object ID of the UIViewController and "FDu-oq-UpZ" is the Object ID of the UIView.

However, you may also use...

NSString *viewControllerName = [[NSString alloc] initWithString:viewController.title];

This will return the "Title" of the UIViewController in the Attributes Inspector; so just as easily as you added the Storyboard ID to the UIViewController, you may also add a title.

Solution 6 - Ios

You can compare with class name . import class and then try.

NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *root = [viewControllers objectAtIndex:0];
if ([root isKindOfClass:[UserLogin class]]) {
//--- do ---
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJalakooView Question on Stackoverflow
Solution 1 - IosLolloz89View Answer on Stackoverflow
Solution 2 - IosErickson1View Answer on Stackoverflow
Solution 3 - IosLombaXView Answer on Stackoverflow
Solution 4 - IosApple_iOS0304View Answer on Stackoverflow
Solution 5 - Iosserge-kView Answer on Stackoverflow
Solution 6 - IosDeepak Kumar SahuView Answer on Stackoverflow