1

I have this code :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
        let controller = SectionsController()
        self.navigationController?.pushViewController(controller, animated: true)
}

In my class :

class HomeController: UITabBarController,UITableViewDataSource,UITableViewDelegate, AddingProtocoleDelegate 

And i want to change the back button title to "Back"

I've tried this in the sectionController viewDidLoad :

        self.navigationItem.backBarButtonItem?.title = "Back"
        self.navigationController?.navigationBar.backItem?.title = "Back"
        self.navigationController?.navigationItem.title = "Back"
        let backButton = UIBarButtonItem(
            title: "Back",
            style: UIBarButtonItemStyle.plain,
            target: nil,
            action: nil
        );
        self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton;

But any solutions didn't work.

Help please.

3
  • Did you set rood view as a navigation controller? Mar 18, 2017 at 13:16
  • 1
    Possible duplicate of how to create back button in navigation bar Mar 18, 2017 at 13:20
  • Nazmul, i set the home controller as root view controller, but i don't know how t oset the sectioncontroller as the new root view controller with the .pushViewController method Mar 18, 2017 at 14:10

2 Answers 2

1

use this self.navigationItem.leftBarButtonItem = backButton

3
  • This just create a new left button in my navigation bar, but not a back button, just a button with a title of "back" Mar 18, 2017 at 14:12
  • I found, i must use a selector in the button in action, and in the targeted function I call self.navigationController?.popViewController(animated: true). But a have a problem, Xcode say me that Expression of type 'UIviewController?' is unused Mar 18, 2017 at 14:17
  • I found again ^, i must say _ = self.navigationcontroller?.popViewController(animated : true) because this method return a ViewController. Mar 18, 2017 at 14:21
1

The back button belongs to the previous view controller, not the one currently presented on screen. Try this and see if it works for you.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let backItem = UIBarButtonItem()
backItem.title = "Back"
navigationItem.backBarButtonItem = backItem
}

Swift 3:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let backItem = UIBarButtonItem()
backItem.title = "Back"
navigationItem.backBarButtonItem = backItem
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.