0
votes

In have a UITabBarController with several UIViewControllers. Inside one of the controllers, when a certain condition is met, I want to instantiate another UIViewController which is a child of the same UITabBarController. I keep getting this error "Application tried to present modally an active controller", but I don't understand how is scheduleNavController already active. I looked up several answers on SO, but I still don't understand what is my mistake and how can I solve it? The flow of the app is like this: WelcomeViewController,LoginViewController,UITabBarController and the children of the UITabBarController.

   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let tabController = storyboard.instantiateViewController(withIdentifier: "CentralTabBarControllerID") as! UITabBarController

 if let viewControllers = tabBarController?.viewControllers {
     let scheduleNavController = viewControllers[1] as! UINavigationController
     let scheduleVC = scheduleNavController.childViewControllers[0] as! Schedule

       tabController.present(scheduleNavController, animated: true, completion: {
          scheduleVC.segmentedControlIndexReceivedFromClaimDetail = self.segmentedControlIndex
             })

        }

enter image description here

1
You retrieve scheduleNavController from the array of active view controllers and then try and present it morally.Paulw11
@Paulw11 What should I do then ?bibscy
Why not simply change the selectedIndex of your tab bar controller?Andreas Oetjen
@AndreasOetjen I have used your suggested method. However, could you please post an answer fixing the code from my original question without using selectedIndex? I have seen lots of questions asked using the method I tried in my original question. I think such answer would be helpful for other SO members. Thanksbibscy
Changing the selected index is the correct approach though;Paulw11

1 Answers

1
votes

Thanks to Andreas Oetjen suggestion to use the selectedIndex of the UITabBarController I have come up with another solution. However, I still don't know exactly how I should fix my code from the original question to make it work.

 //select index of the UIViewController we want to switch to 
// get the UINavigationController for the tab we want to switch to
// get UIViewController to which we want to pass data 
 self.tabBarController?.selectedIndex = 1
 let scheduleNavController = tabBarController?.viewControllers?[1] as! UINavigationController
 let scheduleVC = scheduleNavController.childViewControllers[0] as! Schedule
 scheduleVC.segmentedControlIndexReceivedFromClaimDetail = self.segmentedControlIndex

//remove the the current UIViewController from which we switch to another controller above.
 let claimDetailNav = tabBarController?.viewControllers?[0] as! UINavigationController
  let claimDetailVC = claimDetailNav.childViewControllers[1] as! ClaimDetail
  claimDetailVC.removeFromParentViewController()