在实例化视图控制器中使用NSMutableAttributedString引用UIButton时遇到问题

时间:2018-12-10 19:21:16

标签: swift uibutton nsattributedstring nsmutableattributedstring

一旦加载了视图控制器,就会看到一个按钮(灰色背景,带有白色字体),显示文本“ Sto 1”。在viewWillLayoutSubviews中调用此方法,并使用NSMutableAttributedString设置标题。 “ Sto”是商店的缩写。

对于我的应用程序,我希望用户能够选择Sto 1按钮并能够存储UILabel上显示的数字。我可以获取当前显示的数字,但是无法使用NSMutableAttributedString更新我的Sto 1按钮中的文本。换句话说,我想从显示“ Sto 1”的按钮转到显示一些数字(例如12)。

感谢大家为我提供的任何帮助。我对Swift还是比较陌生,过去一周我一直在尝试解决此问题。

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)

}}

1 个答案:

答案 0 :(得分:2)

通常,您将使用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)
}