【问题标题】:I just instantiated the ViewController, why is its IBOutlet nil?我刚刚实例化了 ViewController,为什么它的 IBOutlet 为 nil?
【发布时间】:2021-06-07 22:47:57
【问题描述】:
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
vc.label.text = "my Label" // Crashes here, label is nil

如果我需要在这里使用它怎么办?

vc.lable is nil

There's an IBOutlet in the ViewController

【问题讨论】:

  • 你怎么知道它是零?显示你的真实代码。
  • 确保您引用了正确的视图控制器。一个常见错误:Storyboard 中的两个控制器,都分配了类 ViewController ... one 被赋予了标识符“ViewController” ...但随后添加了标签并且@IBOutlet 连接是发给其他 ViewController.
  • 我确定,因为项目中只有一个ViewController

标签: ios swift xcode uikit iboutlet


【解决方案1】:

将代码编辑器的 UILabel @IBOutlet 连接到情节提要上的 UILabel:

【讨论】:

  • 这个IBOutlet是通过连接生成的
  • 您可以尝试删除生成的连接,然后使用上述步骤重新连接。
  • 我已经尝试并重新创建了一个项目测试,它仍然是这样。
猜你喜欢
  • 2023-03-26
  • 2017-10-27
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2020-12-04
  • 1970-01-01
相关资源
最近更新 更多