iOS Build a Simple iPhone App with Swift View Controllers and Views Creating IBOutlets

Kanish Chhabra
Kanish Chhabra
2,151 Points

What if I create IBOutlet in the viewDidLoad method? Why won't it work?

What if I create IBOutlet in the viewDidLoad method? Why won't it work?

1 Answer

An iboutlet is just a reference to an objet on the storyboard, you want these above your view did load to make it accessible throughout your whole class. You can modify it in your view did load, but I can't see any reason why you would need to create it there.

However, if you're looking to create an object in your viewdidload, like a UILabel, you can always do it without an outlet.

override func viewDidLoad() {

      let label = UILabel()
      label.frame = CGRect(0,0,somewidth,someheight)
      view.addSubview(label)

}
Kanish Chhabra
Kanish Chhabra
2,151 Points

Thanks Jeremy, just asked out of curiosity, didn't feel any need to create it in the viewdidload.??

Nothing wrong with curiosity! Glad I could help.