Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left Bar Button Item - show name of previous View Controller

If you have a Navigation Controller with no Bar Button Items, a navigation back button will be shown with the name of the last View Controller.

I want to keep that name, as in I don't want to have to hardcode it. I do know how to add it in but I don't want to have to do that because that leaves more room for bugs.

Is there a way that I can have a left Bar Button Item and for the default one to not go away?

like image 237
Mikael Weiss Avatar asked Apr 06 '17 11:04

Mikael Weiss


People also ask

How do I set the back button title of the controller?

The Back button reads the controller underneath the current controller for its title. You cannot set the back button title directly. Instead, you set the title of the current controller before you leave for the destination controller. If the previous view controller’s title is nil, the button titles itself Back.

How do I add a bar button to the navigation bar?

To do this press cmd, shift and l, and then search for bar button item: Once you have found the bar button item drag it into the navigation bar, make sure to add one to the left and the right hand sides: Now that we have are bar button items in the navigation bar, we create actions in our code.

How do I set the navigation item title on the toolbar?

The navigationItem property is an instance of UINavigationItem, which has four major properties: a title, a left bar button, a right bar button and a prompt. To set the title on the toolbar , you set the string for the title property. For example add this to the ViewController class Build and run. The root view now has One as a title.

How do I dismiss a view controller in UIViewController?

Call methods of UIViewController directly. Each technique gives you different amounts of control over the presentation and dismissal process. Using segues in your storyboard is the recommended way to present and dismiss view controllers. A segue is a visual representation of a transition from one view controller to another.


1 Answers

Add this in viewController where you want to have default back button and custom bar button item. You can customise the bar button item.

override func viewDidLoad() {
    super.viewDidLoad()
    let newBtn = UIBarButtonItem(title: "new", style: .plain, target: self, action: #selector(anotherMethod))
    self.navigationItem.leftItemsSupplementBackButton = true
    self.navigationItem.leftBarButtonItem = newBtn//self.navigationItem.leftBarButtonItems = [newBtn,anotherBtn]
}
like image 52
RajeshKumar R Avatar answered Jan 04 '23 19:01

RajeshKumar R