0

So, in Xcode Swift code, you simply drag a UI component to the text editor and create an IBOutlet like @IBOutlet weak var myLabel:UILabel

My questions are:

  1. I read that IBOutlet resolve to nothing during compile time. It's only a keyword for Xcode itself. I assume Xcode generated some code when I drag the UI. So, where is the code and what does it look like?
  2. Say, if I don't have Xcode, where/how should I write code to connect UI with it's behavior programmatically?
  3. After all, is it possible to write, compile, deploy IOS projects without Xcode?

Thanks,

This related question answers about how to program in Xcode not using its storyboard by configuring Xcode.

Community
  • 1
  • 1
Weishi Z
  • 1,629
  • 5
  • 18
  • 38

1 Answers1

0

True, IBOutlet doesn't do anything. In Objective-C this is defined as a macro and will be replaced by nothing while compiling. There is no special code. Interface Builder on the other hand sees this and allows connections for those properties to be configured. The information which outlet property is connected to which object then is saved in the nib file while compiling. After the system loaded all those objects it steps through the saved connection and sets the outlet properties to the appropriate objects using the regular setValue:forKey: mechanism.

You can see this in action if you implement a custom setter for one of your outlets and set a breakpoint on that.

All this is only necessary to support Interface Builder. If you don't use it you don't need the concept of outlets - you just keep references to the objects you need later after you created them.

The same thing also applies to IBAction. In Objective-C this is again defined to be void via a macro. Interface builder sees them and allows you to make connections there. While the nib files are loaded these are connected by sending the addTarget:action:forControlEvents: message to the control this action is connected to. If you don't use IB you can send this message yourself to make the connection.

Sven
  • 22,475
  • 4
  • 52
  • 71