拥有一个带有多个UIViewControllers的UITabBarController。在其中一个控制器内部,当满足某个条件时,我想实例化另一个UIViewController,它是同一个UITabBarController的子节点。 我一直收到这个错误" 应用程序尝试以模态方式呈现一个活动控制器"但我不明白scheduleNavController是如何处于活动状态的。我在SO上查了几个答案,但我仍然不明白我的错误是什么,我该如何解决呢? 应用程序的流程如下:WelcomeViewController,LoginViewController,UITabBarController和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
})
}
答案 0 :(得分:0)
感谢Andreas Oetjen建议使用UITabBarController的selectedIndex
,我提出了另一个解决方案。但是,我仍然不知道应该如何修复原始问题中的代码以使其正常工作。
//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()