Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).
I want to change the text shown on the button to something else.
I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.
self.navigationItem.leftBarButtonItem.title = @"Log Out";
What should I do?
Thanks.
Stan's answer was the best one. But it also have a problem, when you use the controller with a Tab Bar and change the controller's title, you could change the Tab Bar's title too.So the best answer is change the view_controller.navigationItem.title only and use the view_controller.navigationItem.title in the function. Answer is here:(With ARC and add them into view's viewDidLoad)
In myself use, I make it a function like this, just have the feature with one line code in the viewDidLoad.
Im new in iOS but I will provide my very simple answer of overriding the navigation controller class. I have simple override the push and pop methods and save the title of previous view controller. Sorry for pasting in js block. Was little confused how to past it in normal code block.
you can put it whereever you like in the code in the parrent controller, which allowes you to have differenct backbuttons for different child views.
If you want not only to change the text of the Back button to the same text and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".
This code works too. Put this on the root controller of the navigation controller:
ok. I personally hated all of these options. Therefore I came up with my own.
Based on the information I have seen. It appears that the Previous view controller is in control of its own "Back" button that will be presented on the pushed view controller.
I have created a Lazy Load method for the navigationItem on the controller that wants the changed Back Button.
Mine is an Invite Buyer Controller
Invite Buyer is the text that is set by default.
but the back button needed to be Invite
Here is the code that I used to create the back button.
I placed this code in the top of the Controller's Implementatio (.m) file and it overrode the super's method automatically.
I feel this is a much more elegant way to accomplish this.
I place this code in one place, and it automatically gets populated when needed.
No need to call the code before each push request.
Hope this helps