如何在iOS中更改標簽以與圖像對應? - How to change label to correspond with image in iOS? -开发者知识库

如何在iOS中更改標簽以與圖像對應? - How to change label to correspond with image in iOS? -开发者知识库,第1张

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.

我正在嘗試制作一個基本的應用程序卡片游戲,我想更改我的標簽中的文本以與繪制的卡片相對應。例如,當繪制Ace時,我希望標簽文本顯示“ACE”。現在我已經編寫了下面的代碼,它會在每次點擊“繪圖”按鈕時隨機抽取卡片並隨機抽取一張卡片。

@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)

1 个解决方案

#1


1  

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語句,例如

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

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复