schmru schmru - 1 year ago 66
iOS Question

Opening and closing UINavigationController

I know it was probably written somewhere about this problem already but I just can't find the solution and am on the problem already for a few days.

I have an application with UITabbar with 4 UINavigationControllers.
On one of this UINavigationControllers there is a UIViewController with a button that opens with modal transition another UINavigationController. On opening everything works normal, but after closing last NC from its UIViewController with code:

self.dismiss(animated: false, completion: nil)


self.navigationController?.dismiss(animated: false, completion: nil)

I get a problem.
Whatever the next thing I do, I get a warning Unbalanced calls to begin/end appearance transitions for and on UIViewControllers methods viewWillAppear and viewDidAppear don't get called. But just to make it clear, I don't get this problem only when I open a new view, I also get it if I just switch between tabs to other UINavigationViewController.

I have checked one of the possibilities that I've read about and I' sure that I don't open one thing twice.

I just can't figure out if it is the problem of multiple NavigationControllers or the way I close it or what.

Any suggestion will be helpful.


Forgot to tell, that the UINavigationController is opened with modal segue, not by code.

All the NC have at least one VC (all 4 on the tabbar and also the one opened later)


The code that dismisses the VC is run on the last opened VC on last opened NC (not one of the tabbar NC) to return to one of the tabbar NC/ his VC.
To make sure I'll try to write it again

| | | |
VC - the one that calls dismiss to return to previous VC

It is just so frustrating that until I open another UINavigationController everything works great, but after that the problem begins. Or too add another thing I noticed, the problem appears after modal presenting another controller, it doesn't matter if it is a UINavigation or just normal ViewController.


Thanks to @kgkoutio the problem was solved, the mistake I made was that I did't call super.viewDidLoad and super.viewWillAppear somewhere. After adding it to all of VC the problem disappeared.

Again big thanks to @kgkoutio

Answer Source

Your code dismisses the NC not the VC. When the app is started the TabBar is initialized with the set of your Navigation controllers. Consider dismissing the UIViewController instead of NC:

self.navigationController.topViewController?.dismiss(animated: false, completion: nil)