Swift 5 UIButton 标题没有改变

Swift 5 UIButton title didn't change

我刚刚学习 Swift 并开发了一个 iOS 项目。

但是当我点击它时按钮标题没有改变。如何更改标题?

模拟器:iPhone11iOS14.4

这是代码:

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()
    }
}

您没有在代码中调用函数 showAlert()。 您需要在用户单击该所需按钮时立即调用该函数,为此您需要使用一个函数,该函数会在单击操作发生并调用和处理您所需的功能时立即调用。

上面的代码运行正常,只是标题变了。您还可以更新警报处理程序中的标题,如下所示。

   @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)}

找到原因了!

因为UIButton的标题是【Attributed】或者【Plain】。

✅Different attribute has different API to change the title

如果是【归因】: 我们应该通过以下方式更改 UIButton 标题:

//
//  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.
    }


}


如果是【普通】:

//
//  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.
    }


}

感谢所有帮助过的人!

我发现在 Target > DeploymentInfo > iPhone = 将 iOS 版本更改为目前我使用的最新版本 15.0,从 12.0 更改。 这就是为什么我的按钮标题在安装到设备后没有变粗的原因。