Swift 3.0
Global declaration of the class,
let circleArray = ["circle-gray-big","circle-green-big","circle-red-big"]
var index = 0
In UIImageView
:
@IBOutlet var imageView: UIImageView! //Global
@IBAction func carbsButton(_ sender: Any) {
self.index = (self.index >= self.circleArray.count-1) ? 0 : self.index+1
self.imageView.image = UIImage(named:circleArray[index])
}
In UIButton
Change Any
in carbsButton(_ sender: Any)
to carbsButton(_ sender: UIButton)
. After do like below code,
@IBAction func carbsButton(_ sender: UIButton) {
self.index = (self.index >= self.circleArray.count-1) ? 0 : self.index+1
sender.setImage(UIImage(named:circleArray[index]), for: .normal)//Button
}
In UIImageView and UIButton
:
let circleArray = ["circle-gray-big","circle-green-big","circle-red-big"]
var index = 0
@IBOutlet var imageView: UIImageView!
@IBAction func carbsButton(_ sender: UIButton) {
self.index = (self.index >= self.circleArray.count-1) ? 0 : self.index+1
self.imageView.image = UIImage(named:circleArray[index]) //imageView
sender.setImage(UIImage(named:circleArray[index]), for: .normal)//Button
}
Sample Output:
sender.setBackgroundImage(UIImage(named:circleArray[index]), for: .normal)