How do you use Interface builder with Swift?
Asked Answered
P

5

14

When hooking Swift code up to a Storyboard, how do you add the IBAction and IBOutlet tags?

Parthenia answered 3/6, 2014 at 10:56 Comment(3)
same way, ctrl+drag inside class block, then you choose if it's outlet or actionAnnisannissa
@CalinChitu: Can you elaborate? I usually wire things up in the other direction - from IB to code.Parthenia
yes, from IB to code, same as before. Just the interface it's not there anymore so you drag inside class to your code. And you have to pick if it's an outlet or actionAnnisannissa
P
31

Add IBAction and IBOutlet attributes to variables and functions so they can be visible in Interface builder.

class ViewController: UIViewController {

    @IBOutlet var label: UILabel?

    @IBAction func doTap(x:UIButton) {
        println("Tapped: \(x)")
    }
}
Parthenia answered 3/6, 2014 at 10:56 Comment(7)
You can also use @IBDesignable and @IBInspectable if you're developing your own controls. :)Castorina
True. The Swift book also mentions these two new attributes, but I guess we have to wait for the NDA to be lifted before we can say more about them.Parthenia
Actually we can publically discuss this: oleb.net/blog/2014/06/apple-lifted-beta-ndaCaptor
I tried something like this and can't make it work. I see the outlet circle in the gutter of the ViewController.swift, but can't drag from it to the button or control-drag from the view to swift code... wonder if i'm doing something obviously wrong or it's a bug of some sortTiffie
The way I do it (and I'm sure there has to be a better way) is: 1) select the object in IB, 2) in the far-right pane select "connection inspector", 3) under "New Referencing Outlet" click the circle and drag it all the way to the view controller that has the @IBOutlet in the left-pane of IB.Parthenia
I was doing it right, it's a bug in Xcode. I was trying to do it in an existing project and when i open the project, it was giving me a "something is wrong, no further information". When i create a completely new project it works wellTiffie
Shouldn't it @IBOutlet weak var label: UILabel! ?Dropforge
C
4

Below code shows IBOutlet and IBAction format in Swift :

class MyViewController: UIViewController {

  @IBOutlet weak var btnSomeButton: UIButton?
  @IBOutlet weak var lblLabelItem: UILabel?

  @IBAction func btnSomeButtonClicked(sender: UIButton) {
    ...
  }
}

You can bind them same way as done in Objective-C.

Conjugated answered 12/2, 2015 at 11:28 Comment(0)
B
3

Just use old ctrl + drag technique which was popular in Xcode5 and everything works fine.

Bonina answered 3/6, 2014 at 13:54 Comment(1)
If the control-drag technique isn't working, make sure you have the class in IB set to match the name of your custom swift view controller class.Cerise
L
3

I would agree more with Jayprakash than the upvoted first answer. The only thing I would correct is the marking of the IBOutlets as implicitly unwrapped with the ! The first answer used to be correct, but several changes were made in Swift and how it interacts with IB in the latest release. In Swift, IBOutlets no longer have any implicit behavior or magic--they are simply annotations for IB. As of the date of this response, the following code is correct:

// How to specify an the equivalent of IBOutletCollection in Swift
@IBOutlet var fields: [UITextField]!
// How to specify a standard IBOutlet
@IBOutlet weak var button: UIButton!
// How to specify an IBAction
@IBAction func buttonWasPressed(sender: UIButton) { ... } 
Liggitt answered 21/2, 2015 at 5:11 Comment(0)
L
1

While creating a project, you should have selected the storyboard, so that you can add your IBOutlet's directly in the story board.

The Below code gives you a idea of how to add IBOutlet to the UILabel

class ViewController: UIViewController {

    @IBOutlet var label : UILabel

}
Lovelace answered 25/6, 2014 at 11:15 Comment(1)
IBOutlet has non-optional type UIButtonOutworn

© 2022 - 2024 — McMap. All rights reserved.