【问题标题】:How can I access a view controller's storyboard id programatically如何以编程方式访问视图控制器故事板 ID
【发布时间】:2019-01-28 00:22:43
【问题描述】:

我尝试过self.view.accessibilityIdentifierstoryboard?.accessibilityAttributedLabel,它们都不包含情节提要的 id 值。那是什么?

编辑:我发现这个问题的答案已经过时,对于 swift 4 或最近的 xcode 版本没有任何意义

How to know the current storyboard name?

【问题讨论】:

    标签: swift xcode storyboard


    【解决方案1】:

    如果您在身份检查器中将“使用 Storyboard ID”设置为“是”,则这在 ViewDidLoad 中有效:

    if let str = self.restorationIdentifier {
    //Do something with the View Controller's Storyboard ID
    }
    

    【讨论】:

    • 为什么将str 声明为String! 而不是String??或者更好的是,只需 let str = self.restorationIdentifier
    【解决方案2】:
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "endGame") as! EndViewController
    
            present(vc, animated: false, completion: nil)
    

    在身份检查器的故事板上输入您的故事板 ID,我的是“endGame”,您还需要添加一个新的可可触摸类,然后在身份检查器中您还需要在其中添加该 ViewControllers 名称。我的是 EndViewController。然后使用上面的代码。

    【讨论】:

    • 只是补充一点,请确保在您的情节提要上选择了您希望访问的 ViewController。例如,在我的代码中,我正在访问游戏的结束屏幕。所以在我玩 ViewController 时,我将该代码输入到结束游戏的函数中。所以当用户结束时,他们会访问我的 EndGame ViewController。
    猜你喜欢
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2017-12-15
    • 1970-01-01
    • 2013-11-19
    相关资源
    最近更新 更多