Programmatically get a Storyboard ID?

后端 未结 6 1072
醉话见心
醉话见心 2020-12-02 21:52

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

UIViewController *aViewController;
NSString *storyboardID =         


        
相关标签:
6条回答
  • 2020-12-02 22:18

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

    0 讨论(0)
  • 2020-12-02 22:21

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-02 22:21

    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.

    0 讨论(0)
  • 2020-12-02 22:27

    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?

    0 讨论(0)
  • 2020-12-02 22:33

    You can use the Restoration ID:

    NSString *restorationId = self.restorationIdentifier;
    

    Just check the checkbox 'Use Storyboard ID'

    0 讨论(0)
  • 2020-12-02 22:34

    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 ---
    }
    
    0 讨论(0)
提交回复
热议问题