3

In Objective C it was easy to change the image of a UIView based on a tag using a cast to (UIImageView*) — something like:

[ (UIImageView*) [self.view viewWithTag:n+1] setImage:[UIImage imageNamed:@"bb.png"]];

I've been trying to find an up-to-date example that would do the same thing in Swift 3. I finally came on the solution — you need to use the as! keyword and optionally cast to UIImageView?. if you need to do this — here's some working code:

// SWIFT3 change UIView based on viewWithTag (johnrpenner, toronto)

override func viewDidLoad() 
{
    super.viewDidLoad()

    var n : Int = 0
    for i in stride(from:7, to:-1, by:-1) {
        for j in 0...7 {
            n = i*8+j

            let sqImageView = UIImageView(image: UIImage(named: "wp.png"))
            sqImageView.tag = n+1

            sqImageView.frame.origin.x = (CGFloat(j) * cellX + xStartAdj)
            sqImageView.frame.origin.y = ( (CGFloat(7-i) * cellY) + yStartAdj )

            view.addSubview(sqImageView)
            }
        }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
//func touchesBegan(touches: Set<UITouch>, with event: UIEvent?)
{
    if let touch = touches.first {
        let touchPos = touch.location(in: self.view)  //swift3
        let touchSQ : Int = coordToSQ(touchPoint:touchPos)

        // change UIView.image based on viewWithTag
        if var findView : UIImageView = view.viewWithTag(touchSQ+1) as! UIImageView? { findView.image = UIImage(named: "bp.png")! }     
    }
}

Any how — it took a couple hours to figure this out, and everything out there is for Swift 2 or Objective C — thought a Swift 3 example might be useful.

cheers! john p

Nirav D
  • 71,513
  • 12
  • 161
  • 183
johnrpenner
  • 175
  • 1
  • 8

2 Answers2

2

Simply like this.

if let findView = self.view.viewWithTag(touchSQ+1) as? UIImageView {
    findView.image = UIImage(named: "bp.png")
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • it was working with var, although let is better form. – johnrpenner Nov 16 '16 at 07:28
  • If you are not mutating the value then it is batter to use `let` :) – Nirav D Nov 16 '16 at 07:29
  • it was working with var, although let is better form. the if-let detection wasnt really the problem though - there's plenty of examples with that - the part that wasnt obvious was that in order to change the uiview, one first has to cast as! UIImageView? (or without the ? when using let). you could alter uiview.hidden and everything but the image without the as? wouldnt work, – johnrpenner Nov 16 '16 at 07:35
  • @johnrpenner You have to cast the `UIView` to `UIImageView` using `as` and `?` is use for optional binding instead of `!` that is force wrapping it may cause you crash if it is not `imageView`. – Nirav D Nov 16 '16 at 10:50
0

thx nirav — using let .. as? UIImageView .. is the good answer.

this might be a useful function:

func setImageView(byTag:Int, toImageNamed:String)
{
    if let findView = self.view.viewWithTag(byTag) as? UIImageView {findView.image = UIImage(named: toImageNamed)}
    return
}
johnrpenner
  • 175
  • 1
  • 8
  • Can you please tell me what is difference between my answer and your, you have written same just by adding function? – Nirav D Nov 17 '16 at 04:25