一旦我的视图控制器加载,我就会看到一个按钮(灰色背景,白色字体),显示文本“sto 1”。这在viewwilllayoutsubviews中调用,标题使用nsmutableattributedstring设置。“sto”是store的缩写。
对于我的应用程序,我希望用户能够选择sto 1按钮并能够存储uilabel上显示的数字。我可以获取当前显示的号码,但无法使用nsmutableattributedstring更新sto 1按钮中的文本。换句话说,我想从显示“sto 1”的按钮转到显示一些数字(例如,12)。
谢谢你们能为我提供帮助。我对斯威夫特还比较陌生,过去一周我一直在努力解决这个问题。

import UIKit

class ViewController: UIViewController {


var fontConstant = CGFloat()
var someRandomNumberDisplayedOnAUILabel = String(12)

@IBOutlet weak var myButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillLayoutSubviews() {

    fontConstant = 1
    let myString = "Sto 1"
    let myAttributes = [NSAttributedString.Key.foregroundColor :  UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
    let mutableAttributedString = NSMutableAttributedString(string: myString, attributes: myAttributes)
    myButton.setAttributedTitle(mutableAttributedString, for: .normal)
    myButton.backgroundColor = UIColor(red: 94/255.0, green: 94/255.0, blue: 94/255.0, alpha: 1.0)

}


@IBAction func myButtonAction(_ sender: Any) {

    let myAttributes = [NSAttributedString.Key.foregroundColor :  UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
    let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)
    myButton.setAttributedTitle(mutableAttributedString, for: .normal)
    myButton.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)

}}

最佳答案

通常,您可以使用setTitle(:for:)更改UIButton上的文本。但由于您使用的是NSMutableAttributedString函数,因此需要setAttributedTitle(:for:)函数。我想这可能就是你要找的:

myButton.setAttributedTitle(myNSMutableAttributedString, for: .normal)

不过,还是要抬头。您可能需要为不同的控件状态调用此函数,而不仅仅是.normal,否则当按钮突出显示时,您可能会看到不同的文本。Here是控件状态的列表。
编辑:我会尝试在IBAction而不是myButton中引用发送者。这可能是一个快速的解决方案:
@IBAction func myButtonAction(_ sender: Any) {

    let myAttributes = [NSAttributedString.Key.foregroundColor :  UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
    let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)

    guard let button = sender as? UIButton else {
        print("Error: sender was not a button")
        return
    }
    button.setAttributedTitle(mutableAttributedString, for: .normal)
    button.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)

}

编辑2:如果您丢失了对IBOutlet的引用,您可以在丢失之前为按钮分配一个选择器来解决这个问题。试试这个:
override func viewDidLoad() {
    super.viewDidLoad()

    // Add the action to the button rather than holding on to the IBOutlet
    myButton.addTarget(self, action: #selector(RideInProgressViewController.myAction(sender:)), for: .touchUpInside)
}

@objc private func myAction(sender: Any) {
    guard let button = sender as? UIButton else {
        print("Error: sender was not a button")
        return
    }

    let myAttributes = [NSAttributedString.Key.foregroundColor :  UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
    let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)

    button.setAttributedTitle(mutableAttributedString, for: .normal)
    button.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)
}

关于swift - 在实例化 View Controller 中使用NSMutableAttributedString引用UIButton时遇到问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53712259/

10-13 08:28