Changing image of UIButton via click - XCode 6 Swift
swift, xcode
Solution
The way you're setting up the control is incorrect. Assuming you have a button property named `btnShell` (and it's the button you want to setup) change your viewDidLoad() method to:
override func viewDidLoad()
{
super.viewDidLoad()
btnShell.setImage(imgShell1, forState:.Normal);
btnShell.setImage(coin, forState:.Highlighted);
}
And then remove the setImage(_:forState:) call from the action method:
@IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
Problem
Im simply trying to make it so when I click on a UIButton (for which it currently shows the image of a shell), the image changes into something else (in this case, a coin). This is what i tried so far and have not had any success. I cant find anything to do this for Swift.Thanks. ``` import UIKit class ViewController: UIViewController { @IBOutlet weak var lblOutput: UILabel! @IBOutlet weak var lblWin: UILabel! @IBOutlet weak var lblLost: UILabel! @IBOutlet weak var lblWinsAmt: UILabel! @IBOutlet weak var lblLossesAmt: UILabel! let coin = UIImage(named: "penny_head") as UIImage; override func viewDidLoad() { super.viewDidLoad() //imgShell1.hidden = true //doesnt work } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func btnStart(sender: UIButton) { } @IBAction func btnShell1(sender: UIButton) { sender.setImage(coin,forState: UIControlState.Highlighted); } ```