27

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
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
KSL
  • 307
  • 1
  • 3
  • 6

6 Answers6

40

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))
Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58
Kirsteins
  • 27,065
  • 8
  • 76
  • 78
17

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

}
rvg
  • 893
  • 8
  • 11
  • In case you want to initialize UIBarButtonItem programmatically in Swift 4+ should be done as following: editButton = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editAct)). So already with title, but not with system style. – Serj Rubens Sep 24 '19 at 19:00
5

Swift 4:

@IBOutlet var saveButton: UIBarButtonItem!

saveButton.title = "Saved"
Joel
  • 327
  • 4
  • 3
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.

fractalwrench
  • 4,028
  • 7
  • 33
  • 49
Manu
  • 629
  • 7
  • 6
0

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

Omar N Shamali
  • 503
  • 5
  • 11
-1

from the docs

init(title title: String?,
 style style: UIBarButtonItemStyle,
target target: AnyObject?,
action action: Selector)
RyanTCB
  • 7,400
  • 5
  • 42
  • 62
  • This is not an answer. This is the constructor for the UIBarButton. I am sure the OP knew that. Please avoid "answers" of this kind – Juanjo Jun 01 '20 at 21:34
  • @Juanjo to quote the Question “I’m a beginner”. Don’t assume the OP knows anything and my answer answers their question by showing how to set a title of a button – RyanTCB Jun 01 '20 at 21:38
  • No, it does not. If you think the constructor signature is relevant state why and how this method can solve the OP question. Just copying and pasting a method is not an answer. – Juanjo Jun 01 '20 at 21:42
  • If you read the Question the OP is using an init they are just not using the right one. The one I have replied with. One that allows the OP to set the title. Thus answering the question asked. – RyanTCB Jun 02 '20 at 05:38
  • And If you look at the accepted answer (which was posted exact same time as mine) it is the same constructor as mine. So clearly it’s you that is wrong and not at all helpful. – RyanTCB Jun 02 '20 at 05:42
  • The accepted answer states why this constructor helps and posts another one as an update and clearly states that. "as per doc: " is not an answer. At least not a complete one. If you want to help be helpful don't just post to see if you get "points" I am being very specific why I downvoted your answer. – Juanjo Jun 02 '20 at 14:41
  • Apples code is self documenting. You just want comments and faff – RyanTCB Jun 02 '20 at 14:46