Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.