簡體   English   中英

Swift 5 UIButton 標題沒變

[英]Swift 5 UIButton title didn't change

剛剛學習了Swift,開發了一個iOS的項目。

但是當我點擊它時按鈕標題不會改變。 如何更改標題?

模擬器:iPhone 11 iOS14.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()
    }
}

您尚未在代碼中調用 function showAlert()。 您需要在用戶單擊所需按鈕后立即調用 function,為此您需要使用 function,一旦發生單擊操作並調用和處理您所需的功能,就會調用它。

上面的代碼運行良好,並且標題發生了變化。 您還可以更新警報處理程序中的標題,如下所示。

   @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】。

✅不同的屬性有不同的API改標題

如果是【Attributed】:我們應該把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 更改為 15.0。 這就是為什么我的按鈕標題在安裝到設備后沒有變粗的原因。 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM