Swift put multiple IBOutlets in an Array Swift put multiple IBOutlets in an Array ios ios

Swift put multiple IBOutlets in an Array


you can define a generic outlet collection in Swift like this:

@IBOutlet var collectionOfViews: Array<UIView>? // = [UIView]?

or for e.g. UIButton objects:

@IBOutlet var collectionOfButtons: Array<UIButton>? // = [UIButton]?

you can find your collections under the Outlet Collections group as usually are in the File's Owner:

Outlet Collections

it would look on my console after connecting 5 random buttons:

Connected UIButton instances to Collection


Follow these steps to create an array of outlets an connect it with IB Elements:

  • Create an array of IBOutlets
  • Add multiple UIElements (Views) in your Storyboard ViewController interface
  • Select ViewController (In storyboard) and open connection inspector
  • There is option 'Outlet Collections' in connection inspector (You will see an array of outlets there)
  • Connect if with your interface elements

-

class ViewController2: UIViewController {    @IBOutlet var collection:[UIView]!    override func viewDidLoad() {        super.viewDidLoad()    }}

enter image description here