74

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Jalakoo
  • 3,523
  • 2
  • 21
  • 20

6 Answers6

94

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

Lolloz89
  • 2,809
  • 2
  • 26
  • 41
  • For anyone making apps or static libraries for earlier iOS versions, the restorationIdentifier property is iOS 6.0+ – Jalakoo Dec 09 '14 at 23:33
  • 7
    Just tick "Use Storyboard ID" in Interface builder to automatically use the storyboard ID as the restorationIdentifier property. It will be set dynamically without you having to type it twice. – gchbib Dec 17 '14 at 11:11
  • 4
    [Reference of restorationIdentifier](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/restorationIdentifier) says `The value of this property is nil by default, which indicates that the view’s state does not need to be saved`. This means setting restorationIdentifier to other than nil has some kind of side effects! – ypresto Nov 25 '15 at 10:44
61

You can use the Restoration ID:

NSString *restorationId = self.restorationIdentifier;

Just check the checkbox 'Use Storyboard ID'

Erickson1
  • 623
  • 6
  • 6
21

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?

LombaX
  • 17,265
  • 5
  • 52
  • 77
  • 1
    Exploring different ways of uniquely identifying viewControllers and their views programmatically. .tag, .title, .accessibilityLabel, .nibName all work well enough. Object Ids I believe are out (private?), so was wondering if Storyboard Ids were at all an option. – Jalakoo Dec 04 '12 at 18:42
  • 1
    Maybe you can "generate" the identifier using controller class name – ıɾuǝʞ Aug 19 '13 at 09:56
  • I use more than 40 view controllers in project and I have no intent to set some new value to each of them just to be locatable. Restoration ID is set up already for me. – vedrano Jul 15 '14 at 14:19
  • Apple added the "Use Storyboard ID" checkbox for the Restoration identifier in the Interface Builder. They obviously did that to make it easier for us developers to use the storyboard identifier in code. – gchbib Dec 17 '14 at 11:10
  • May be using one controller code file and multiple ViewController. – Mohammad Zaid Pathan Mar 12 '16 at 20:00
13

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
    }
}
kelin
  • 11,323
  • 6
  • 67
  • 104
Apple_iOS0304
  • 1,092
  • 1
  • 9
  • 19
1

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.

serge-k
  • 3,394
  • 2
  • 24
  • 55
0

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