KSL
KSL

Reputation: 307

How do I set title for UIBarButtonItem?

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
}

Upvotes: 27

Views: 35524

Answers (6)

Omar N Shamali
Omar N Shamali

Reputation: 773

If anyone wondering how to do it from the storyboard: Try to edit the "Title" attribute in "Attributes Inspector" as shown below: enter image description here

Upvotes: 0

Joel
Joel

Reputation: 357

Swift 4:

@IBOutlet var saveButton: UIBarButtonItem!

saveButton.title = "Saved"

Upvotes: 6

Kirsteins
Kirsteins

Reputation: 27335

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

Upvotes: 40

Manu
Manu

Reputation: 647

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.

Upvotes: 3

user691369
user691369

Reputation:

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.
    }

}

Upvotes: 17

RyanTCB
RyanTCB

Reputation: 8224

from the docs

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

Upvotes: -1

Related Questions