Transition within Navigation to the Root Tab Bar Controller

I have an issue of moving from a ViewController back to a root tab bar controller. Here's my storyboard, I have attached an image of my issue.

I have tried the following:

Code Block
let homeTBC = controllers![3] as! UITabBarController
let homeVC = homeTBC.viewControllers?[0] as! HomeViewController
self.present(homeVC, animated: true, completion: nil)


I'm getting this error message:

Code Block *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally a view controller <ITEiOSApp.HomeViewController: 0x7fbb200bf000> that has a parent view controller <UITabBarController: 0x7fbb200bea00>.'
terminating with uncaught exception of type NSException


Please help really running out of options here.




Here's a screenshot of my Story Board:


Code Block
https://drive.google.com/file/d/1s_6lzdRs3_FyDRSEv6Nk3BLCdAkIWfy1/view



It is pretty hard to understand from the storyboard image.

Please describe the full path that goes from root to the last VC:
  • what are the types of each intermediate VC: NavController ? plain VC ?

First seems to be a TabBarController. Exact ?
Then you select second tab. Exact ?
From there you go to a TableViewController. Exact ?
4th and 5th seem to be plain VC. Exact ?
  • Which is HomeViewController ?

  • how you navigate from each to the next ?

Please explain in detail, show code if it is not a direct segue.

In the small code snippet:
Code Block
let homeTBC = controllers![3] as! UITabBarController
let homeVC = homeTBC.viewControllers?[0] as! HomeViewController
self.present(homeVC, animated: true, completion: nil)


In which VC is this ? I understand the last one ?
What are controllers ? How are they defined ? Where are they defined ?
What is controllers![3] supposed to be ?
Which line exactly does the crash occur ? Could you post more of the crash log ?

Please answer precisely to all questions if you want some help.
Hi Claude,

Sorry for the lack of information provided, here a link to a diagram that made show to the overview of my storyboard

Code Block
https://drive.google.com/file/d/1Jpt38FRSBhTo4LcMEDQARA1Of94IxHLj/view?usp=sharing


To better explain my current scenario, I have a Navigation Controller as my InitialVC this navigation handles the Login and Signup VC, once user is successfully login the screen will segue to a ServicesTableViewController. When a cell is tapped in the TVC, the TabBarController will show. My Tab Bar Controller has 5 VC: Home, Bill, Usage, Shop and Support

To answer you question above, the hierarchy is this:
  • Navigation Controller

    • LoginEnrollVC

    • EnrollVC

    • LoginVC

    • ServicesTVC

    • TabBarController

- HomeVC
  • Bill VC -> PaymentAccountVC -> AddCardVC -> PaymentSuccessVC

- Usage VC
  • Shop VC

- SupportVC

The issue at hand is when I reached PaymentSuccessVC and try to go back to my TabBarController:Home that's is whey the error message started to appear.

My Payment Success VC has a button that shows, Go Back To Home and that is where I put this portion of the code:

Code Block
let controllers = self.navigationController.viewcontrollers
let homeTBC = controllers![3] as! UITabBarController
let homeVC = homeTBC.viewControllers?[0] as! HomeViewController
self.present(homeVC, animated: true, completion: nil)


but when I try clicking that button, it's showing this error message:
Code Block
terminating with uncaught exception of type NSException


Here's some additional screenshot of my story board for more reference:

Code Block
https://drive.google.com/file/d/1yp-tnKqwSu7rcccLoBttlPzD6FlODdZK/view?usp=sharing
https://drive.google.com/file/d/1O0gNE1G-2kB44j77pmIWgRwPD8Gr3lA4/view?usp=sharing
https://drive.google.com/file/d/1Rr6uJaEmA_VD8kEKPnsvwXbBnNlT5-et/view?usp=sharing
https://drive.google.com/file/d/18T2VUg-OVm7Qdvw-Dx80hdH72UTYJuD_/view?usp=sharing
https://drive.google.com/file/d/1V68CPJG7Bn4TbPvcMrnjVsyKo9xOaat4/view?usp=sharing


Thank you so much in advance

To add this is how I get to my Payment SuccessVC:

Navigation Controlller -> LoginEnrollVC -> LoginVC -> ServiceTVC -> TabBarController: HomeVC -> TabBarController: BillVC (Pay Now Button) -> Show Payment Account VC (Add payment Button) -> Add Card VC (Pay Now Button) -> Payment Success (Go Back To Home Button) -> TabBarController: HomeVC

The error message happens when I try to go back to my Tab Bar Controller.

I hope I was able to answer all your questions. Thanks again.
Is it the real code ? It probably does not compile:
Code Block
let controllers = self.navigationController.viewcontrollers
let homeTBC = controllers![3] as! UITabBarController
let homeVC = homeTBC.viewControllers?[0] as! HomeViewController
self.present(homeVC, animated: true, completion: nil)


Line 1, it should be navigationController?.viewControllers and not navigationController.viewcontrollers

I don't understand your line 2 and the view hierarchy:

What I understand (looking at MyDiagram:
Navigation Controller > LoginEnrollVC > EnrollVC > LoginVC > ServicesTVC > TabBarController with 5 tabs
                                                  –––––––––––––––––––––––––––––––––––––––––––––––––––––––––|
                                                 |
                                   TabBarController
___________________________|_____________________________________________
|                |                            |                                   |                                     |
Home BillVC          PaymentAccountVC AddCardVC PaymentSuccessVC

You call self.navigationController.viewcontrollers, in PaymentSuccessVC
Who is this PaymentSuccessVC.navigationController ? Is it Home, or is there a navigation controller ahead of Home ?
there should be a navigation controller
Is there a nav controller above of below BillVC ? Otherwise, I do not see who is the navController of SuccessPaymentVC.
In such a case, code could be like:

Code Block
let homeTBC = self.navigationController?.tabBarController // the TabBarController
let homeVC = homeTBC.viewControllers?[0] as! HomeViewController
self.present(homeVC, animated: true, completion: nil)

In addition I don't understand is who is controllers[3] ? Looks like it is self ????

So, I miss something in your architecture.
I tried to reassemble the full storyboard from the 5 files… That didn't match well.

Could you post a complete view of the storyboard (event in reduced zoom, but with clear label of what each VC is ?
Transition within Navigation to the Root Tab Bar Controller
 
 
Q