有一个带有几个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
})
}
最佳答案
感谢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()
关于ios - 错误应用程序尝试以模态形式呈现事件 Controller Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49298209/