1

I'm creating an app that will control climate. The value changes, however, I cannot get different (Guage) images to display based on value. please help!

Here is my code

 /* Fan Output Up */

@IBAction func FanOutputUpBtn(_ sender: Any) {

    let name = "Fan Position\(fanCounter)"
    /* Setting the name of the fan position image including the variable fanCounter */

    fanPositionImage.image = UIImage(named: "fanPosition \(fanCounter)")
    /* Setting the name of the fan position image including the variable fanCounter */

    if fanCounter == 5 {
        /* Set maximum value */
    } else {

    fanCounter += 1
        /* Increase the fan counter position by 1 */

    TestFanCounter.text = String(Int(self.fanCounter))
        /* Display the counter image */

        self.fanPositionImage.image = UIImage(named: name)
        /* Display image for fan position based on fan counter */


}

Update

Now It's random images displaying on tap however the TestFanCounter.text = String(Int(self.fanCounter)) is displaying completly fine

   /* Fan Output Up */

@IBAction func FanOutputUpBtn(_ sender: Any) {

    let name = "Fan Position \(fanCounter)"
    // Setting the name of the fan position image including the variable fanCounter

    if fanCounter == 4 {
        // Set maximum value
    } else {

    fanCounter += 1
        // Increase the fan counter position by 1

    TestFanCounter.text = String(Int(self.fanCounter))
        // Display the counter image

        self.fanPositionImage.image = UIImage(named: name)
        /* Display image for fan position based on fan counter */

    }

}

B.Slade
  • 143
  • 1
  • 8
  • 1
    You're setting the same image twice here, is that intentional? Also the naming schema for your images changes between the first and second assignment. – David Jul 07 '17 at 23:00
  • Hi David, I see, I have changed this now but it's completely randomising the images displayed – B.Slade Jul 08 '17 at 00:43

1 Answers1

1

You are incrementing the fanCounter after you set the name. When you display the counter, it will be one higher than when you assigned the name, which you are using to set an image. Are those values supposed to be equal?

  • If you move the counter to the bottom, you will have set all of your values using the same fanCounter value and then increment afterwards. – Phillip Key Jul 08 '17 at 01:44
  • That worked, thank you so much. Only one issue remains, 0 won't show the image allocated to it, I don't suppose you have any idea why? – B.Slade Jul 08 '17 at 01:50
  • If i were going to take a stab in the dark, the base image is probably named "Fan Position" and this code produces "Fan Position 0" – Phillip Key Jul 08 '17 at 01:55
  • I have named the images as follows (Fan Position 0, Fan Position 1, Fan Position 2, Fan Position 3, Fan Position 4). All others are showing correctly apart from Fan Position 0. – B.Slade Jul 08 '17 at 01:59
  • Problem solved, I missed a capital letter in Fan Position 0. Thanks again for your help, it's much appreciated. – B.Slade Jul 08 '17 at 02:00
  • Glad I could help. – Phillip Key Jul 08 '17 at 02:07