需要用按钮生成的 Int 更改 UIImageView Swift

Need to change UIImageView with a Int generated by a button Swift

我正在开发一个可以控制气候的应用程序。值发生变化,但是,我无法根据值显示不同的 (Guage) 图像。请帮助!

这是我的代码

 /* 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 */


}

更新

现在点击时显示随机图像,但是 TestFanCounter.text = String(Int(self.fanCounter)) 显示完全正常

   /* 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 */

    }

}

您在设置名称后增加了 fanCounter。当您显示计数器时,它会比您分配名称时高一个,您将使用该名称来设置图像。这些值应该相等吗?