Search code examples
iosswiftswift5

Swift 5 UIButton title didn't change


I just learned Swift and developed an iOS project.

But the button title doesn't change when I click it. How can I change the title?

Simulator: iPhone 11 iOS14.4enter image description here

enter image description here enter image description here This is the code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var helloButton: UIButton!
    
    @IBAction func showAlert(_ sender: Any) {
    //var alert = UIAlertController(title: "Hello!", message: "Hello, world!",preferredStyle: UIAlertControllerStyle.Alert)
    let alert = UIAlertController(title: "Hello", message: "Hello, world", preferredStyle: UIAlertController.Style.alert)
        
    alert.addAction(UIAlertAction(title: "close", style: UIAlertAction.Style.default, handler: nil))
        
    self.present(alert,animated:true,completion:nil)
    // ❌ it didn't work
    self.helloButton.setTitle("Clicked",for:.normal)     
    }
    
    override func viewDidLoad() {
      super.viewDidLoad()
    }
}

Solution

  • I found the reason!

    It was because the UIButton title being 【Attributed】 or 【Plain】.

    ✅Different attribute has different API to change the title

    If it is 【Attributed】: We should change the UIButton title by:

    //
    //  ViewController.swift
    //  HelloCocoa
    //
    //  Created by LearnChild on 2021/4/14.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var helloButton: UIButton!
        
        
        @IBAction func showAlert(_ sender: Any) {
    //        var alert = UIAlertController(title: "Hello!", message: "Hello, world!",preferredStyle: UIAlertControllerStyle.Alert)
            let alert = UIAlertController(title: "Hello", message: "Hello, world", preferredStyle: UIAlertController.Style.alert)
    
            alert.addAction(UIAlertAction(title: "close", style: UIAlertAction.Style.default, handler: nil))
    
            self.present(alert,animated:true,completion:nil)
    
            //        if UIButton title is Plain
    //        self.helloButton.setTitle("Clicked",for:.normal)
            
            
            
    //        if UIButton title is Attributed✅👇
            let myNormalAttributedTitle = NSAttributedString(string: "Click Here",
                                                             attributes: [NSAttributedString.Key.foregroundColor : UIColor.blue])
            self.helloButton.setAttributedTitle(myNormalAttributedTitle,for:.normal)
    
    
        }
    
        
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
    
    
    }
    
    
    

    And if it's 【Plain】:

    //
    //  ViewController.swift
    //  HelloCocoa
    //
    //  Created by LearnChild on 2021/4/14.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var helloButton: UIButton!
        
        
        @IBAction func showAlert(_ sender: Any) {
    //        var alert = UIAlertController(title: "Hello!", message: "Hello, world!",preferredStyle: UIAlertControllerStyle.Alert)
            let alert = UIAlertController(title: "Hello", message: "Hello, world", preferredStyle: UIAlertController.Style.alert)
    
            alert.addAction(UIAlertAction(title: "close", style: UIAlertAction.Style.default, handler: nil))
    
            self.present(alert,animated:true,completion:nil)
    
            //        if UIButton title is Plain✅👇
            self.helloButton.setTitle("Clicked",for:.normal)
            
            
            
    //        if UIButton title is Attributed✅
    //        let myNormalAttributedTitle = NSAttributedString(string: "Click Here",
    //                                                         attributes: [NSAttributedString.Key.foregroundColor : UIColor.blue])
    //        self.helloButton.setAttributedTitle(myNormalAttributedTitle,for:.normal)
    
    
        }
    
        
    
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
    
    
    }
    
    

    enter image description here

    Thanks to everyone who helped!