Search code examples
swiftxcodestoryboardviewcontrolleriboutlet

how to create one iboutlet for multiple buttons in swift


So far I have only been able to create one iboutlet per button. What i want to achieve is to create one iboutlet for many buttons thus allowing me to write the code for creating the same border width, colour and radius around these buttons only once. Is this possible?

import UIKit
           
            class GameScreenViewController: UIViewController {
                @IBOutlet weak var answerTextLabel: UILabel!
                @IBOutlet weak var scoreLabel: UILabel!
                @IBOutlet weak var questionLabel: UILabel!
                @IBOutlet weak var numberBtn: UIButton! //this iboutlet is what i would like to be able to connect to multiple number buttons in my app.
    }

Solution

  • you can use IBoutlet Collection !

    enter image description here

    enter image description here

    You can connect several same types of objects after declaring as IBoutlet Collection.

    And if you want to run the same code, you can use the for statement.

    for item in myButton {
         item.layer.cornerRadius = 3
     }