title: Modifying images of generated imageViews on button click
body: <p>I am creating a quiz app and having trouble changing the image which is displayed of a specific imageview that is generated in a for loop.</p>
  
  <p>The following code is responsible for generating the imageviews based on the amount of questions in that specific quiz. It is called in the <code>viewDidLoad()</code> method:</p>
  
  <pre><code>var imageView = UIImageView()
  
  func addProgressImages(to view: UIView, count: Int){
      let screenSize = UIScreen.main.bounds.size
  
      for i in 0..&lt;count {
          let spacing: CGFloat = 2
          let tag = i + 888
          let width: CGFloat = screenSize.width/CGFloat(count)
          let height: CGFloat = width
          if let view = view.viewWithTag(tag){
              view.removeFromSuperview()
          }
          let x = spacing + CGFloat(i) * width
          imageView = UIImageView(frame: CGRect(x: x, y: 100, width: width - spacing, height: height))
          imageView.image = UIImage(named: "question_progress_empty")
          imageView.tag = tag
          imageView.contentMode = .scaleAspectFit
          view.addSubview(imageView)
          //print("imageviews displaying")
      }
  }
  </code></pre>
  
  <p>I have made an attempt to change some of the images to another image when the user selects an answer to that question with an if statement:</p>
  
  <pre><code>    func changeImage() {
      if (optA != questionAns){
      imageView.image = UIImage(named: "wrong_answer")
          print("opta")
      }else if(optB == questionAns){
          imageView.image = UIImage(named: "wrong_answer")
          print("optb")
      }else if(optC == questionAns){
          print("optc")
          imageView.image = UIImage(named: "wrong_answer")
      }else{
          print("if statement not initialised")
      }
  }
  </code></pre>
  
  <p><a href="https://imgur.com/a/zwJ4EVn" rel="nofollow noreferrer">https://imgur.com/a/zwJ4EVn</a> - This is the output of the above code
  Currently, only the last image will be changed when a user clicks a button.</p>
  
  <p>I need help with modifying the imageview which is related to that specific question, e.g. if question 2 is answered, only that imageview will change while the rest remain unmodified.</p>
  
  <p>Hopefully that explanation made any sense, any input would be appreciated,
  Thanks.</p>