iOS XCode Noob Question: IB linking to Code

arkmannj

macrumors 68000
Original poster
Oct 1, 2003
1,574
329
UT
Hello,
I am just starting to learn Swift and XCode, and I had a quick question.
I'm just learning about linking items in the Interface Builder (From a storyboard or XIB) to code by Ctrl+Click & dragging from the item in IB into the code, where XCode then places some code beginning with something like: @IBOiutlet, @IBAction.

My question is this, Is the Click+Drag business necessary? Could I have just typed in those lines of code and had them be connected? Or is Xcode also doing other things in the background that I cannot see to facilitate those connections?

Thanks!
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
Long story short, placing button in IB and doing the right-click/control+click to the source code is just easier.

You could absolutely create the the button in code and specify where it should be placed and it's action. You also could drag a button in IB, give it a tag, then have your code go through the view hierarchy looking for an object with that tag, but that is just silly. Stick with the control+click
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
If you type in the IBOutlet declaration into your code you can click+drag to connect the view to the IBOutlet declaration in the code. You can also click to connect outlets directly in IB without having the code visible.

Click+Drag lets you both create the IBOutlet declaration and connect it. But there are a few ways to connect an IBOutlet if it already exists in your code. Simply typing in the IBOutlet declaration doesn't connect it to anything. You have to connect outlets using IB.
 

moonman239

macrumors 68000
Mar 27, 2009
1,524
22
Creating an IBOutlet for a view is kind of like giving the view a name that you can use to access it in your code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.