-1
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
vc.label.text = "my Label" // Crashes here, label is nil

What if I need to use it here?

vc.lable is nil

There's an IBOutlet in the ViewController

  • How do you know it is nil? Show your real code. – matt Mar 10 '21 at 02:29
  • Make sure you're referencing the correct view controller. A common mistake: two Controllers in Storyboard, both assigned the class `ViewController` ... **one** is given the Identifier "ViewController" ... but then the label is added and the `@IBOutlet` connection is made to the ***other*** `ViewController`. – DonMag Mar 10 '21 at 13:42
  • I'm sure, because there's only one ViewController in the project – 醉翁之意 Mar 11 '21 at 01:50

1 Answers1

0

Connect your Code Editor's UILabel @IBOutlet to the UILabel on the Storyboard:

enter image description here

mjoe7
  • 134
  • 6