0

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!]
Adam G.
  • 107
  • 1
  • 8
  • You could also create an `IBOutlet` for a collection. `@IBOutlet var myImageViews: [UIImageView]!` and link each `UIImageView` to the collection. You will be able to also keep an `IBOutlet` for each individual `UIImageView` if you ever need to reference a specific view. – Caleb Aug 13 '15 at 02:47

2 Answers2

1

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.

Fujia
  • 1,232
  • 9
  • 14
  • Thanks for the answer, I successfully put the code in and control-dragged my uiimageviews, but now I am getting errors when I try testing it in IOS Sim.? It says something like: _Thread 1: Signal SIGABRT_ Would you know any reason why this is happening? – Adam G. Aug 13 '15 at 03:15
  • 1
    SIGABRT means some error happened outside of your code. I guess you have made some individual outlets for those UIImageViews before trying my answer. So you probably have deleted those outlets from your code (source file), but still left them in the storyboard file. Try to clear those outlets in storyboard (In the connection inspector at the right most side of the Utilities pane). – Fujia Aug 13 '15 at 03:27
0

You just create an array of UIImageViews:

@IBOutlet weak var T1: UIImageView!
@IBOutlet weak var T2: UIImageView!
@IBOutlet weak var T3: UIImageView!

let array = [UIImageView]()
array.append(T1)
array.append(T2)
array.append(T3)
Darko
  • 9,655
  • 9
  • 36
  • 48