How do I set title for UIBarButtonItem?

我怕爱的太早我们不能终老 提交于 2020-05-12 11:08:08

问题


I'm a beginner iPhone developer.

How can I programmatically set the title for the UIBarButtonItem?

My code is the following:

self.navigationItem.rightBarButtonItems =
    UIBarButtonItem(
        barButtonSystemItem: .Cancel, target: self,
        action: "barButtonItemClicked:")

@IBAction func barButtonItemClicked(sender: UIBarButtonItem) {
    //print something
}

回答1:


Use different initialiser that allows you to specify the title:

UIBarButtonItem(title: "title", style: .Plain, target: self, action: "barButtonItemClicked:")

Swift 3.1 Update

UIBarButtonItem(title: "title", style: .plain, target: self, action: #selector(barButtonItemClicked))



回答2:


Swift 2 answer

You could just add the following to viewDidLoad:

// Some text
self.barButtonItemClicked.title = "Right Button!"

OR

// A Unicode Gear Symbol
// See: http://graphemica.com/%E2%9A%99
self.barButtonItemClicked.title = "\u{2699}"

The ViewController.Swift code below would set the name barButtonItemClicked you used. I used a UIBarButtonItem to show how to set the title. But it is trivial to adapt it for a function.

import UIKit

class ViewController: UIViewController {

    // Please make sure the line below is properly connected to your Storyboard!
    @IBOutlet weak var barButtonItemClicked: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

       // Set the text of the NavBarButtonItem Title  
       self.barButtonItemClicked.title = "Right Button!"

       // Gear Icon
       // self.barButtonItemClicked.title = "\u{2699}"

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}



回答3:


In my case, I wanted to toggle between the Edit|Done. However, I couldn’t use the leftBarButtonItem because I already had another UIBarButtonItem.

What I did is the following:

1- Create @IBOutlet weak var edit: UIBarButtonItem!

2- Then a variable to hold the state: var isEditingMode = false

3- Now in the viewDidLoad:

override func viewDidLoad() {
    …
    self.edit.action = #selector(self.toogleEditor(_:))
    self.edit.title = "Edit"
    self.setEditing(isEditingMode, animated: true)
    …
}

I initialize the edit.action Selector to my custom function toogleEditor. I want to be able to change the title whenever an action occur.

4- Create an IBAction:

@IBAction func toogleEditor(sender: AnyObject) {

    if isEditingMode
    {
        isEditingMode = false
        self.edit.title = "Edit"
    }
    else
    {
        isEditingMode = true
        self.edit.title = "Done"
    }
    self.setEditing(isEditingMode, animated: true)
}

This function is triggered each time the user click the UIBarItemButton. The only thing to do is use the setEditing(…) to change the behaviour of the UITableViewController.




回答4:


Swift 4:

@IBOutlet var saveButton: UIBarButtonItem!

saveButton.title = "Saved"



回答5:


from the docs

init(title title: String?,
 style style: UIBarButtonItemStyle,
target target: AnyObject?,
action action: Selector)


来源:https://stackoverflow.com/questions/28210818/how-do-i-set-title-for-uibarbuttonitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!