Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiated NavigationController from storyboard reference the same RootViewController

I'm instantiating a Navigation Controller from storyboard for which i have already defined the root view controller.

If i instantiate multiple navigationcontroller as follows:

        let nav = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CategorizedArticleNavigationController") as! UINavigationController
        let rootVC = nav.viewControllers.first as! CategorizedArticlesViewController
        let nav_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CategorizedArticleNavigationController") as! UINavigationController
        let rootVC_1 = nav.viewControllers.first as! CategorizedArticlesViewController            
        self.present(rootVC, animated: true, completion: nil)

I get an exception after trying to present modally :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller ...'

After debugging, to my surprise, i found that nav != nav_1

but

rootVC == rootVC_1

And with that i get an exception trying to present modally the navigation because it finds out that its root vc is already being presented.

Has any one seen this ? should i file a bug ? or is this somehow in purpose ?

UPDATE: This was definitely an error on my part, @Samantha hepled clarify that

enter image description here

Thank you.

like image 259
TheFuquan Avatar asked Aug 02 '26 02:08

TheFuquan


1 Answers

I think the reason they're the same is this line:

let rootVC_1 = nav.viewControllers.first as! CategorizedArticlesViewController            

Here, you're assigning rootVC_1 to the original navigation controller's root view controller (so, the same object as rootVC). It should be

let rootVC_1 = nav_1.viewControllers.first as! CategorizedArticlesViewController            
like image 140
Samantha Avatar answered Aug 03 '26 19:08

Samantha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!