gpt4 book ai didi

ios - 如何访问和更改由 UIButton 组成的自定义 barButtonItem 的 NSAttributedString 标题?

转载 作者:行者123 更新时间:2023-11-30 13:15:34 24 4
gpt4 key购买 nike

我在工具栏中创建了一个自定义 barButtonItem。我想在点击 barButtonItem 时更改 NSAttributedString 的字符串。但是正如苹果文档所说,该功能的性能是由customView而不是barButtonItem负责的,我如何根据事件访问并对其细节进行一些更改?

    let button1 = UIButton(type: .System)
button1.sizeToFit()
let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
let attributedString1 = NSAttributedString(string: "Hello", attributes: attributes1)
button1.setAttributedTitle(attributedString1, forState: .Normal)
button1.addTarget(self, action: #selector(ActionViewController.switchAccounts), forControlEvents: .TouchUpInside)
let account = UIBarButtonItem(customView: button1)

setToolbarItems([account], animated: true)

我是一名新手。 OC也可以给我读。欢迎任何建议。谢谢。

最佳答案

您可以像这样使用自定义按钮操作方法

func switchAccounts(sender : UIButton)  {
let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
let attributedString1 = NSAttributedString(string: "world!", attributes: attributes1)

sender.setAttributedTitle(attributedString1, forState: .Normal)
sender.sizeToFit()
}

button1.addTarget(self, action: #selector(ActionViewController.switchAccounts(_:))

而不是

button1.addTarget(self, action: #selector(ActionViewController.switchAccounts)

关于ios - 如何访问和更改由 UIButton 组成的自定义 barButtonItem 的 NSAttributedString 标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38277675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com