2
votes

I want to have a back button on the second view controller I have. When it loads now, it does show the back button but I want to change the title from Back to something else. I implemented to viewWillAppear method where I invoke showing the navigation bar. The following didn't work:

 override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        self.navigationController?.navigationBar.hidden = false
        self.navigationController?.navigationBar.backItem?.title = "Something Else"

    }

Please help me change the title. Should it be in the willAppear or viewDidLoad?

2
Have you tried putting it in viewDidLoad?Arc676
yup didn't work eitherksa_coder
replace the navigationControllers backBarButton with a custom back button...and in the action of back button add code to pop the view Controller.Sujith Chandran

2 Answers

3
votes

Add this right before the push or popViewController statement in your first view:

let backButton = UIBarButtonItem(title: "Something Else",style: UIBarButtonItemStyle.Plain ,target: nil,action: nil)
self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton

Or you can do it in your second view this way:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated);

    let backButton = UIBarButtonItem(title: "Something Else",style: UIBarButtonItemStyle.Plain ,target: nil,action: nil)
    self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton
}
1
votes

Ksa_coder I have tried the solution for your question and finally I got.You want to add the below coding in second view controller in viewWillAppear method

override func viewWillAppear(animated: Bool) 
{

    super.viewWillAppear(animated);


    var btnBack = UIBarButtonItem(title: "Something Else",style: UIBarButtonItemStyle.Plain ,target: nil,action: nil)


    self.navigationController!.navigationBar.topItem!.backBarButtonItem = btnBack

}