How do you use Interface builder with Swift?

后端 未结 5 1661
日久生厌
日久生厌 2021-02-07 12:11

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

5条回答
  •  耶瑟儿~
    2021-02-07 12:46

    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) { ... } 
    

提交回复
热议问题