Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

arkmannj

macrumors 68000
Original poster
Oct 1, 2003
1,760
560
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!
 
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
 
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.
 
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.