Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change back button title when pushing a UIViewController on UITableViewController

View hierarchy:

UINavigationController
      ->UITableViewController(1)
             ->UITableViewController(2)
                   ->UIViewController(3)  

In 1 and 2 I have this code:

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

It should override the back button title of the next view controller pushed on the current view controller. It works from 1 -> 2. But it does not work for 2 -> 3. In 3 the back button title has a title, the name of the previous UITableViewController.

Any ideas whats wrong? I am using swift, xcode6.1 and iOS8.1

like image 541
UpCat Avatar asked Nov 07 '14 09:11

UpCat


1 Answers

You could init a new back button with no title. Just put this in the viewDidLoad() of each view controller.

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

--

I am going to extend this answer with my experience. So I managed to remove the title of the back button to display just the back arrow. In storyboard you have to select the navigation item that displays the title inside the navigation bar of the previous view controller. There is a property called Back Button. Just enter a space and save. It will remove the back button title.

Update UIBarButtonItemStyle.Bordered was deprecated in iOS 8.0. Use UIBarButtonItemStyle.Plain instead.

like image 187
Ron Fessler Avatar answered Oct 19 '22 18:10

Ron Fessler