简体   繁体   中英

Is it possible to programmatically get the identifier of a basic UIViewController from an UIViewController or Storyboard in Swift/Objective-C?

I know that we can instantiate a particular UIViewController basing on its identifier (see below):

var myPageContentViewController = self.storyboard!.instantiateViewControllerWithIdentifier("A") as PageContentViewController

But my question doesn't concern that point, in other words I just would like to know if it would be possible to programmatically (in the source code) retrieve the identifier (in our case the string value "A") that I set in the storyboard for this particular PageContentViewController (which is a subclass of UIViewController in my project)?

Note: an answer using the Swift development language instead of the Objective-C development language would be more convenient for me, even if I know the latter. For your information, I use the 6.0.1 version of Xcode.

Thank you in advance for your answers.

在斯威夫特:

 var str: String! = self.restorationIdentifier

You can get the identifier of the UIViewController by using the restoration identifier with the following code in objective C:

NSString *restorationId = self.restorationIdentifier;

Hope this will help you.

With restorationIdentifier you can get the nil value. There is another way to get the UIViewController identifier, try this

let identifier = String(describing: MyViewController)

But you need to give the same identifier on the storyboard as your UIViewController class name. Please check below example

在此处输入图片说明

  1. Check "Use Storyboard ID" in the storyboard inspector for the respective view controller.

  2. Use the following code:

Swift:

let restorationIdentifier = self.restorationIdentifier

Objective-C:

NSString *restorationIdentifier = self.restorationIdentifier;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM