How do I set title for UIBarButtonItem? How do I set title for UIBarButtonItem? ios ios

How do I set title for UIBarButtonItem?


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))


Swift 2 answer

You could just add the following to viewDidLoad:

// Some textself.barButtonItemClicked.title = "Right Button!"

OR

// A Unicode Gear Symbol// See: http://graphemica.com/%E2%9A%99self.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 UIKitclass 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.    }}


Swift 4:

@IBOutlet var saveButton: UIBarButtonItem!saveButton.title = "Saved"


matomo