繁体   English   中英

是否可以通过 Swift/Objective-C 中的 UIViewController 或 Storyboard 以编程方式获取基本 UIViewController 的标识符?

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

我知道我们可以根据它的标识符实例化一个特定的 UIViewController(见下文):

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

但我的问题与这一点无关,换句话说,我只想知道是否可以以编程方式(在源代码中)检索我设置的标识符(在我们的例子中是字符串值“A”)这个特定 PageContentViewController 的故事板(它是我项目中 UIViewController 的子类)?

注意:使用 Swift 开发语言而不是 Objective-C 开发语言的答案对我来说会更方便,即使我知道后者。 为了您的信息,我使用 6.0.1 版本的 Xcode。

预先感谢您的回答。

在斯威夫特:

 var str: String! = self.restorationIdentifier

您可以通过使用恢复标识符和目标 C 中的以下代码来获取UIViewController的标识符:

NSString *restorationId = self.restorationIdentifier;

希望这会帮助你。

使用restorationIdentifier您可以获得 nil 值。 还有另一种获取UIViewController标识符的方法,试试这个

let identifier = String(describing: MyViewController)

但是您需要在情节UIViewController上提供与UIViewController类名称相同的标识符。 请检查以下示例

在此处输入图片说明

  1. 在相应视图控制器的故事板检查器中选中“使用故事板 ID”。

  2. 使用以下代码:

迅速:

let restorationIdentifier = self.restorationIdentifier

目标-C:

NSString *restorationIdentifier = self.restorationIdentifier;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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