Nico Gutraich Nico Gutraich - 2 years ago 170
Swift Question

How to change the hidden status of a UIImage that is inside an Array in swift?

I'm doing a test app for iPhone (Swift) where my objective is to show multiple images that are currently hidden in a determinate order each time a button is pressed. I thought that i could write:

import UIKit

class ViewController: UIViewController {

var num = -1

var array: [UIImage] = [UIImage(named: "card 1.jpeg")!, UIImage(named: "card 2.jpeg")!, UIImage(named: "card 3.jpeg")!]

@IBAction func button(sender: AnyObject) {

num += 1
array[num].hidden = false


However, "array[num].hidden = false" isn't possible. I want to keep it as simple as possible. Thanks for the help!

Answer Source

UIImage doesn't have the property hidden.

I think you want an array of image views.

@IBOutlet var imageView1: UIImageView!
@IBOutlet var imageView2: UIImageView!
@IBOutlet var imageView3: UIImageView!
var imageViews = [UIImageView]()
var num = 0

override func viewDidLoad() {
    imageViews = [imageView1, imageView2, imageView3]

func button(sender: AnyObject) {

    num += 1
    imageViews[num].hidden = false

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download