Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use Interface builder with Swift?

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

like image 399
Jason Moore Avatar asked Jun 03 '14 10:06

Jason Moore


People also ask

What is Interface Builder in Swift?

Interface Builder The first is an editor, where you write the Swift code that makes your application run. The second is Interface Builder, which is the part where you lay out the graphical interface of your program—the buttons, toolbars, menus, images, and text that make up how your users interact with your program.

Which is better SwiftUI or storyboard?

Conclusion. After looking at the pros and cons of both Storyboard and SwiftUI, according to my opinion building app UI using storyboard is comparatively easier for a beginner, and has been there was a very long time, but there are some flaws with it and that flaws have been taken care of in Swift UI.


2 Answers

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)")
    }
}
like image 147
Jason Moore Avatar answered Sep 22 '22 09:09

Jason Moore


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.

like image 26
Jayprakash Dubey Avatar answered Sep 20 '22 09:09

Jayprakash Dubey