1
votes

I created a table view and from there let say a user pressed a cell it will go to ListTavleView but the only problem right now is that whenever a user is in ListTableView there is not back button even thought i already embed a navigation controller

and i want the fist view navigation bar is small title second view navigation bar is large title

enter image description here

Below is my code

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showList" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let items = dataManager.items[indexPath.row]
            let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
            controller.item = items
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

Below is my storybord setup

enter image description here

Navigation bar with no back button

enter image description here

4
view controller is presenting or pushing?Mahendra
change your segway method to pushWings
Change the Segue to "Push"Keyur Tailor
i change Show(e.g Push) there is no back buttonJosh
Remove the navigation controller in the middle. Simply push the ListTableViewController .Adeel Miraj

4 Answers

3
votes

From the image it seems that view controller is added as a child view controller in current view controller.

There is not need to embedded navigation controller when a cell is pressed becoz there is already a navigation controller at start point so no need to create a new one.(If you present a view controller then you may need to embed navigation controller.)

So the solution is...

  • Delete the navigation controller.

enter image description here

  • Connect directly to the destination view controller without navigation controller as there is already.

enter image description here

1
votes

it is better if you use pushViewController, just get a reference of the other view controller, it will always a back button since you are pushing threw navigation Controller here is a simple example:

let story = UIStoryboard(name: "Main", bundle: nil)
let vc = story.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
self.navigationController?.pushViewController(vc, animated: true)

as for the back button, the issue is with your hierarchy. are you changing the left item of navigation bar in another view controller that might affect navigation bar in your destination view controller.

0
votes

If you wanted to have navigation bar back button on next view, then just push the target view on navigation, it will show default navigation back button. No, need to any extra work.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
  if let indexPath = tableView.indexPathForSelectedRow {
    let items = dataManager.items[indexPath.row]

    guard let controller = segue.destination as? ListTableViewController else {
      return
    }
    controller.item = items
    self.navigationController?.pushViewController(controller, animated: true)
  }
}}

And if you are pushing the viewcontroller with segue, then no need to add below line self.navigationController?.pushViewController(controller, animated: true)

0
votes

You are pushing new NavigationController(say Nav.B) to the existing one(Nav.A).

Each navigation controller keeps different navigation stack. The back button is visible when you add viewcontroller to Navigation controller. Read more about UINavigationController.

For your current scenario, you could delete the second navigation controller(i think it not essential) & connect direct segue to ListTableViewController

So this

let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController

becomes

let controller = segue.destination as! ListTableViewController

When you need large titles(available 11+), you can add this line in viewDidLoad()

navigationController?.navigationBar.prefersLargeTitles = true

And if it needed only for this Viewcontroller, add in viewWillDisappear() or viewDidDisappear()

navigationController?.navigationBar.prefersLargeTitles = false