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

Are IBOutlets global variables?

Are IBOutlets global variables? I have a few of them that I need to access in my Objective-C classes (I have a Swift project), but I can't access global variables.

You’ll have access to anything within a class or protocol that’s marked with the @objc attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:

  • Generics
  • Tuples
  • Enumerations defined in Swift
  • Structures defined in Swift
  • Top-level functions defined in Swift
  • Global variables defined in Swift
  • Typealiases defined in Swift
  • Swift-style variadics
  • Nested types
  • Curried functions

-Apple Docs

1 Answer

IBOutlets are created when the Controller is loaded. For example, in a navigation controller that navigates to a view controller with an IBOutlet of type UILabel, that label will not be available unless the view has been placed onto the stack. If you navigate to another view controller from that source view controller, the destination view controller would be able to access any view controller that has been placed onto the navigation stack.

So yes, they're always accessible, but they don't always have a value.

Another example would be in the prepareForSegue method. If you tried to access an IBOutlet from there, your application would crash because the view controller has not been created.