Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

ios - How to change UIButton image after clicking in Swift?

I'm new to coding development so this should be a pretty noob question. But I'm trying to change the image of my UIButton to an image stored in image.casset "duellogo" if my if statement executes. But I am unsure how to change UIButton Image; I only know how to change UIImageView Image.

import UIKit

class ViewController: UIViewController 
{

    @IBOutlet weak var firstCardImageView: UIImageView!
    @IBOutlet weak var secondCardImageView: UIImageView!
    @IBOutlet weak var playRoundButton: UIButton!
    @IBOutlet weak var backgroundImageView: UIImageView!

    @IBOutlet weak var Player1Score: UILabel!
    @IBOutlet weak var Player2Score: UILabel!

    //declaring index score, 0, to player 1 and player 2
    var player1Total=0
    var player2Total=0

    var cardNamesArray:[String] = ["ace", "card2", "card3", "card4", "card5", "card6", "card7", "card8", "card9", "card10", "jack", "queen", "king"]

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() 
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func playRoundTapped(sender: UIButton) 
    {
        // create variable to make a random number from 1-13
        var firstRandomNumber:Int = Int(arc4random_uniform(13))
        var firstCardString:String = self.cardNamesArray[firstRandomNumber]
        self.firstCardImageView.image = UIImage(named: firstCardString)

        // create variable to make a random number from 1-13 for second card
        var secondRandomNumber = Int(arc4random_uniform(13))
        var secondCardString:String = self.cardNamesArray[secondRandomNumber]
        self.secondCardImageView.image = UIImage(named: secondCardString)


        // Determine the higher card
        if firstRandomNumber > secondRandomNumber{
            //TODO: first card is lager
            player1Total+=1
            self.Player1Score.text = String(player1Total)
        }else if(firstRandomNumber == secondRandomNumber){
            // ERROR: TRYING TO CHANGE BUTTON IMAGE
            self.playRoundButton.setImage("duellogo", forState: UIControlState.Normal)
            //    ^^ ERROR
        }else{
            //TODO: second card is larger
            player2Total+=1
            self.Player2Score.text = String(player2Total)
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this code

let image = UIImage(named: "duellogo.png") as UIImage!
self.playRoundButton.setImage(image, forState: .Normal)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...