skip to Main Content

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

2

Answers


  1. 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
     }
    
    Login or Signup to reply.
  2. The above solution recommended is indeed correct. There is an option for connecting collection of IBOutlets. But you need to make sure all the IBOutlet it connected to the collection are of the same type. For example collection of UIButtons only.

    You connect using New Referencing Outlet Collection instead of New Referencing Outlet

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search