Search code examples
arraysswiftobjectcontrolsinstances

Swift Arrays with Controls/Instances


Is it possible to have arrays of UIImageViews? If so, how?

I wrote this thinking it would work, but I get errors (so it's definitely wrong):

let Array_of_UIImageViews = [@IBOutlet weak var T1: UIImageView!,@IBOutlet weak var T2: UIImageView!,@IBOutlet weak var T3: UIImageView!]

Solution

  • If you want to let Interface Builder automatically link IBOutlets to a collection, declare the collection like this:

    @IBOutlet var imageViews: [UIImageView]!
    

    Then you can control-drag all your interested outlets from storyboard one by one to this line of code.

    Alternatively you can control-drag an outlet and choose Outlet Collection as the connection type in the popup. It will generated exactly the same code mentioned above. Then, you can control-drag other interested outlets to the generated line of code.