Swift 5 UIButton title didn't change
Asked Answered
H

4

2

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()
    }
}
Heterosexuality answered 15/4, 2021 at 5:53 Comment(3)
What are you trying to do, you have a function showAlert that open on button tap right? and same button title you want to change right?Boric
I want the effect is that: 1. show the button 「Hello」 2. clicked the button and alert a window 3. after clicked the button title from 「Hello」 change 「Clicked」Heterosexuality
It can alert window, but cant change UIbutton title after Clicked. All screen only have one UIButtonHeterosexuality
H
2

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!

Heterosexuality answered 15/4, 2021 at 11:33 Comment(0)
N
0

You haven't called for the function showAlert() in the code. You need to call the function as soon as the user clicks on that required button, for which you'll need to use a function which is called as soon as the click action happens and your required functionality is invoked and processed.

Neuburger answered 15/4, 2021 at 8:53 Comment(1)
Could you help to tell me how to change the code? very thankful!Heterosexuality
D
0

The above code just works fine, and the title changes. You can also update the title inside the alert handler as below.

   @IBAction func helloButtonTapped(_ sender: Any) {
    let alert = UIAlertController(title: "Hello", message: "Hello, world", preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "close", style: .default, handler: { (_) in
        self.helloButton.setTitle("Clicked",for:.normal)
    }))
    self.present(alert,animated:true,completion:nil)}
Dimarco answered 15/4, 2021 at 9:9 Comment(2)
it still cant work normal to change UIbutton title after tap the window in my simulator :(Heterosexuality
working for meeNerti
J
0

enter image description hereenter image description hereWhat i found is that the in Target > DeploymentInfo > iPhone = change iOS version to latest one currently i am using 15.0, changed from 12.0. it was the reason why my button's title was not getting bold after install in device.enter image description here

Jeri answered 27/4, 2022 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.