ios - 如何更改标签以与 iOS 中的图像相对应?

标签 ios swift

我正在尝试制作一个基本的应用纸牌游戏,我想更改标签中的文本以与绘制的纸牌相对应。例如,当绘制 A 时,我希望标签文本显示“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)

最佳答案

因为您提供的代码很少,所以我只能给您一个大概的实现方法。首先,您可以从图像中评估图像的名称(例如图像的文件名),然后您将进行 if 语句,例如

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

关于ios - 如何更改标签以与 iOS 中的图像相对应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35106365/

相关文章:

ios - 从 iOS 上的 Firebase 数据库事件 childChanged 返回的快照

ios - UICollectionView - 为整个 collectionView 添加底部插图

iOS8 如何动态固定 UITableViewHeaderFooterView 的高度大小?

ios - 收到消息 "WARNING: under normal conditions, _fillInQueueWithExtraSpace:.."并且 MPMoviePlayer 旋转在 iPad IOS 5.1 中不起作用

ios - 使用playbackStoreID播放Apple Music歌曲时出现问题

ios - 生成歌曲建议(自动完成)

ios - NSClassFromString swift 2.0 返回的 nil 值

iphone - objective-c : for-loop gets slower and slower

swift - RxSwift 重试全链

swift - Realm swift : Update all model objects and remove migration