C.Kelly
C.Kelly

Reputation: 265

How to change label to correspond with image in iOS?

I am trying to make a basic app card game and I want to change the text in my Label to correspond with the card that is drawn. For example when an Ace is drawn I want the label text to display "ACE". Right now I have written the below code which shuffles the deck and draws a random card each time the "Draw" button is clicked.

@IBAction func playRoundTapped(sender: UIButton) {

    playRoundButton.setTitle("DRAW", forState: UIControlState.Normal)

    var firstRandomNumber = arc4random_uniform(13) + 1
    var firstCardString:String = String(format: "card%i", firstRandomNumber)

    self.firstCardImageView.image = UIImage(named: firstCardString)

Upvotes: 1

Views: 363

Answers (1)

Tom el Safadi
Tom el Safadi

Reputation: 6776

Because there is only little code provided by you I can only give you an apporach of how to do it. Firstly from the image you could evaluate the name of the image (for example the file name of the image) then you will make an if statement and for example

if(filename == "Ace.jpg") {
     // display label
     label.text = "Ace"
}

Upvotes: 1

Related Questions