简体   繁体   中英

iOS Navigation Back Button

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?

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
}

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM