[英]Swift 5 UIButton title didn't change
剛剛學習了Swift,開發了一個iOS的項目。
但是當我點擊它時按鈕標題不會改變。 如何更改標題?
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.
}
}
感謝所有幫助過的人!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.