我在工具栏中做了一个自定义的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)

10-07 14:28