Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the title of the "back" bar button item?

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)
    }
like image 794
Swift Everyday Avatar asked Dec 01 '22 13:12

Swift Everyday


2 Answers

Try to override previous screen segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backItem = UIBarButtonItem()
    backItem.title = "My title"
    navigationItem.backBarButtonItem = backItem
}
like image 87
Oleg Gordiichuk Avatar answered Dec 10 '22 13:12

Oleg Gordiichuk


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

like image 36
Ajinkya Patil Avatar answered Dec 10 '22 13:12

Ajinkya Patil