Lachtan
Lachtan

Reputation: 5172

How to "virtually" tap back button in Navigation Bar

I have app, where is Segmented Control inside of my Navigation Bar. Under navigation bar I have 3 containers. In these containers I have Table View Controllers. If you tap on segmented control, one TVC appear and others disappear (container1.hidden = true and so on).

Problem is when I press "save" button which is also in navigation controller - button doesn't trigger "virtual push of back button".

I used following code which works in my other projects (its in button's action which is in VC that contains all container views) but not this time:

if let navController = self.navigationController {
   navController.popViewControllerAnimated(true)
}

Image for better insight: enter image description here

UPDATE: Thanks to @ Alexey Bondarchuk I solved it. Comments may be confusing so I just recap problem and solution.

Originally, I had ViewController. To this controller I embed in Navigation Controller. To this Navigation Controller I connected segues. And that was mistake. So I deleted this embed in navigation controller, made (show) segues directly to my View Controller (which is on screenshot). This automatically created navigation bar and last thing I did was that I put navigation item in it so now my code pop right navigationController. Hope that it's understandable.

Upvotes: 1

Views: 341

Answers (1)

Alexey Bondarchuk
Alexey Bondarchuk

Reputation: 2022

I have couple ideas:

  1. Your navigationController equal to 'nil' and .popViewControllerAnimated will never invoked. This may happen if you are using UITabBarController. In this case try to use self.tabBarController?.navigationController instead of self.navigationController.

  2. Your controller presented 'Modally'. In this case you can try to invoke navController.dismissViewControllerAnimatedinstead of navController.popViewControllerAnimated

Upvotes: 1

Related Questions