1

I recently started learning iOS development and just loved using the interface builder of Xcode. It's so easy to add segues and outlets and "event handler" thingys (sorry I don't know what they are called).

However, I got confused when I add an outlet of a label or something and sees this gets added in the code

@IBOutlet var weak myLabel: UILabel!

I don't see any assignments to the variable! And when I use the variable, it is not nil!

Back then when I was doing Windows Forms with C#, whenever I added something in the interface designer, some code is generated in a separate file to actually "add" the thing.

I know that storyboard files are just xml files. Whenever I do something to it, the xml changes. But I don't believe xml can do reflection to assign a value to my variable, can it?

So I deduced that there must be some code generated behind the scenes that reflected my code and assigns a value to the outlet variables. Where can I find this file? What exactly is added? Or is my guess wrong?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • The same XIB or storyboard files can be opened as source code. Right click it in File View on the left and select Open As > Source Code. – hannad Dec 21 '15 at 10:51
  • In Storyboard click on you label and in utilities (right panel in xcode) -> attributes inspector you can see all defailt setting for label. You can change it here or in code via your myLabel outlet. – F_Boris Dec 21 '15 at 11:03
  • @F_Boris I know that. What I don't know is how can the variable be assigned and I don't see any assignments. – Sweeper Dec 21 '15 at 11:06

1 Answers1

2

Inside the .storyboard files, the outlets you create are persisted like so:

<connections>
    <outlet property="myLabel" destination="PM2-8V-67p" id="yvq-yW-6rD"/>
</connections>

The property key value pair references the name of the variable it's attached to. The destination KVP points to the element in your storyboard that it's attached to.

Yes, storyboards are simply XML, but when you build your project, they are compiled like any other source code, particularly into compiled NIB files. You'll find a compiled NIB file for each view in your storyboard. Then, when you load your view, it loads the appropriate compiled NIB into memory, creates the actual objects you've created representations for in your storyboard, and hooks up your outlet properties to those objects using the setValue:forKey: accessor method (in iOS). You can find lots more information about the whole NIB lifecycle here:

Mac Developer Library: The Nib Object Life Cycle

As far as where the compiled NIB files exist, you can see them in your build folder (found mine in DerivedData/<YourBuildFolder>/Build/Products/Debug-iphoneos/<YourProjectName>/). Ones generated from storyboards can be found in the Base.lproj folder.

Clay Garrett
  • 1,023
  • 8
  • 11
  • Why are the storyboards called XIB files? Aren't they `.storyboard` files? Is it another file that I need to look for? – Sweeper Dec 21 '15 at 11:15
  • I shouldn't have included references to XIBs in my answer. .XIB files are created when you create just a single view in Interface Builder, whereas storyboards are collections of multiple views, segues, and other objects. In the end, each view in a storyboard winds up as a compiled NIB file. To see these, look inside the DerivedData//Build/Products/Debug-iphoneos//Main.storyboardc package. I'll update my answer to be a bit more clear. – Clay Garrett Dec 21 '15 at 11:58