iOS 에러!

[Swift/iOS] UIButton Title 값이 nil 로 나올 때

녕이 2022. 5. 25. 21:55
728x90

 

 

UIButton을 사용하다 보니 계속 title 값이 nil로 나오는 현상이 발생했다.

 

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet var cardButtons: [UIButton]!
    
    var emojiChoices = ["👻", "🎃"]
    
    func flipCard(withEmoji emoji: String, on button: UIButton){
        print("\(emoji) | \(button.currentTitle)")
        if button.currentTitle == emoji {
            button.setTitle("", for: .normal)
            button.backgroundColor = UIColor.orange
        }else{
            button.setTitle(emoji, for: .normal)
            button.backgroundColor = UIColor.white
        }
    }

    @IBAction func touchCard(_ sender: UIButton) {
        if let cardNumber = cardButtons.firstIndex(of:sender) {
            flipCard(withEmoji: emojiChoices[cardNumber], on: sender)
        }
    }
}

 

→ 클릭한 이모지 배열 속 원소와 해당 버튼의 title을 출력

 

 

 

→ 계속 title이 nil로 출력된다.

 

그래서 inspector 에서 UIButton 속성을 변경해주었는데, Style PlainDefault로 변경해줬다.

 

그랬더니 제대로 작동~

 

 

 

 

 

728x90