当我设置UIButton的标题时,只能通过'UIButton.setTitle()'起作用

时间:2019-07-15 16:05:21

标签: ios swift uibutton

我正在做一些关于swift的简单编码。问题是,我有16个UIButton,它们都是一样的。在for循环中,我给出了表情符号标题朝上的所有按钮。但这是行不通的。最后,只有一个按钮可以显示表情符号。但是在控制台中,他们都可以打印正确的标题!为什么?

@IBOutlet var cardButtons: [UIButton]!

func updateViewFromModel(){
        for index in cardButtons.indices{
            let button = cardButtons[index]
            let card = game.cards[index]
            if card.isFacedUp{
                let thisEmoji = getEmoji(for: card)
                print(thisEmoji)
                button.setTitle(thisEmoji, for: .normal)
                button.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
            }else{
                button.setTitle("", for: .normal)
                if card.isMatched{
                    button.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
                }else{
                    button.backgroundColor = #colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1)
                }
            }
        }
    }

    var emojiChoices = ["?","?","?","?","?","?","?","?","?"]

    var emoji = [Int: String]()

    func getEmoji(for card:Card) -> String{
        if emoji[card.identifier] == nil, emojiChoices.count > 0{
            let randomIndex = Int(arc4random_uniform(UInt32(emojiChoices.count)))
            emoji[card.identifier] = emojiChoices.remove(at: randomIndex)
        }
        return emoji[card.identifier] ?? "?"
    }

1 个答案:

答案 0 :(得分:0)

好的...很好,这是一个愚蠢的问题。我将标题颜色设置为白色,这就是为什么它们无法在屏幕上显示的原因...对不起,您正在观看...