5

I want to change the "Root View Controller" back button to "Back" without changing the title of the Root View Controller.

I have tried these 3 code but all of them do not work.

navigationItem.backBarButtonItem?.title = "Back"
navigationItem.leftBarButtonItem?.title = "Back"
navigationItem.backBarButtonItem?.title = "Back"

May I know what am I doing wrong? Thanks in advance.

Screenshot

Here is the code of the whole method for your reference:

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

    navigationItem.backBarButtonItem?.title = "Back"
    navigationItem.leftBarButtonItem?.title = "Back"

    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest

    if activePlace == -1
    {
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    }
    else
    {
       // print( "String(myvar3.dynamicType) -> \(places[activePlace]["lat"].dynamicType)")
        let latitude = Double(places[activePlace]["lat"]!)
        let longitude = Double(places[activePlace]["lon"]!)

        //creating map region and annotation
        createMapRegionAndAnnotation(latitude!, longitude: longitude!)

        print(latitude)
    }
Swift Everyday
  • 271
  • 1
  • 5
  • 13

4 Answers4

13

Try to override previous screen segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backItem = UIBarButtonItem()
    backItem.title = "My title"
    navigationItem.backBarButtonItem = backItem
}
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • Hi may I know why does this not work?`navigationItem.backBarButtonItem?.title = "Button"` Thank you for your help – Swift Everyday Dec 21 '15 at 13:35
  • 2
    @TeeMingWei If you are setting the back button for the current view controller's navigation item you are not setting the button that get's displayed in the current view. You are in fact setting the back button that will be used if you push another view controller from it. – Oleg Gordiichuk Dec 21 '15 at 14:18
  • 1)doesn't work at all; 2)overriding `prepareForSegue` without of calling `super.prepareForSegue` is incorrect – Gargo Apr 23 '19 at 09:49
5

You can also change it from storyboard by changing the back button property in the navigation item of the previous view controller as follows:

enter image description here

Ajinkya Patil
  • 5,518
  • 4
  • 20
  • 22
1

In child view this worked for me;

self.navigationController!.navigationBar.topItem!.title = "Back"

Phd. Burak Öztürk
  • 1,727
  • 19
  • 29
0

This works for me

let backItem = UIBarButtonItem()
backItem.title = ""
self.navigationItem.backBarButtonItem = backItem
Maulik Vekariya
  • 554
  • 7
  • 19