有一个带有多个 UIViewController 的 UITabBarController。在其中一个 Controller 内,当满足特定条件时,我想实例化另一个 UIViewController,它是同一个 UITabBarController 的子级。 我不断收到此错误“应用程序尝试以模态方式呈现事件 Controller ”,但我不明白 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/