【问题标题】:I can't segue because Xcode is calling my Storyboard ID undeclared我无法继续,因为 Xcode 调用我的 Storyboard ID 未声明
【发布时间】:2020-04-17 21:27:34
【问题描述】:

在 iOS 上,为什么即使我将 UserProfile 分配给我的 StoryboardId,我也会收到以下错误:

使用未声明的类型'UserProfile'

这是我的代码:

let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)

 guard let destinationVC = mainStoryboard.instantiateViewController(withIdentifier: "UserProfile") as? UserProfile else {
            return
        }

 navigationController?.pushViewController(destinationVC, animated: true)

【问题讨论】:

  • 你在哪里实现了 UserProfile 类?
  • 在一个单独的文件中,但该类称为 userProfile
  • 重命名为 UserProfile 或将类型转换为 as?用户资料
  • 错误消失了,但 segue 不工作
  • 您是否在情节提要中为您的 vc 添加了标识符?

标签: ios swift storyboard segue


【解决方案1】:
 guard let destinationVC = mainStoryboard.instantiateViewController(withIdentifier: "YourStoryBoardID") as? YourViewControllerClassName else {
            return
        }

as? YourViewControllerClassName 你需要在这里传递 View Controller 的名称而不是 Storyboard ID。

更新:

由于您当前的视图控制器没有嵌入到 navigationController 中,navigationController?.pushViewController 这将返回 nil 并且该行不会被执行。要执行这一行,您的视图控制器应该有一个导航控制器。

将控制器嵌入导航控制器:Follow point no:1 ,2 ,3 here

如果不想使用navigationController,可以使用presentViewController 代替。

guard let destinationVC = mainStoryboard.instantiateViewController(withIdentifier: "YourStoryBoardID") as? YourViewControllerClassName else {
            return
        }
self.present(destinationVC, animated: true, completion: nil)

Also learn about when to push and when to present a view controller

【讨论】:

  • 修改后没有错误但segue没有任何反应
  • 您是否在情节提要中正确分配了storyboardId 和视图控制器的名称?如果可能的话,附上一张图片。您放置此代码的当前控制器是否嵌入在导航控制器中?
  • 我相信它被正确添加了,但是你如何在导航控制器中嵌入一个新的控制器?
  • 感谢更新成功!你知道在新的视图控制器没有堆叠在前一个视图控制器之上的情况下是否可以这样做?
猜你喜欢
  • 2021-08-14
  • 2013-01-28
  • 1970-01-01
  • 2019-10-14
  • 2018-05-23
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多