【问题标题】:Error Application tried to present modally an active controller Swift错误应用程序试图以模态方式呈现一个活动控制器 Swift
【发布时间】:2018-08-24 04:34:34
【问题描述】:

有一个带有多个 UIViewControllers 的 UITabBarController。在其中一个控制器中,当满足某个条件时,我想实例化另一个 UIViewController,它是同一个 UITabBarController 的子级。 我不断收到此错误“应用程序试图以模态方式呈现活动控制器”,但我不明白 scheduleNavController 是如何处于活动状态的。我在 SO 上查找了几个答案,但我仍然不明白我的错误是什么,我该如何解决? app的流程是这样的:WelcomeViewController,LoginViewController,UITabBarController和UITabBarController的children。

   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
             })

        }

【问题讨论】:

  • 您从活动视图控制器数组中检索scheduleNavController,然后尝试以道德的方式呈现它。
  • @Paulw11 那我该怎么办?
  • 为什么不简单地更改标签栏控制器的selectedIndex
  • @AndreasOetjen 我已经使用了您建议的方法。但是,您能否在不使用selectedIndex 的情况下发布修复我原始问题中的代码的答案?我已经看到很多使用我在原始问题中尝试过的方法提出的问题。我认为这样的答案对其他 SO 成员会有所帮助。谢谢
  • 更改所选索引是正确的方法;

标签: ios swift uitabbarcontroller


【解决方案1】:

感谢 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()

【讨论】:

  • 你不需要呈现它,一旦你改变标签栏控制器的索引它就会显示出来。您可能想要做的是更新显示的数据。
  • @AndreasOetjen 这是在切换到另一个选项卡后删除当前 UIViewController 的正确做法吗?
  • 是的;从技术上讲,您不会删除它,而是将标签索引更改为显示一个不同的索引。小缺点是所有视图控制器都保留在内存中,因此在极少数情况下您可能需要清理一些内存。
  • @AndreasOetjen 我添加了新的代码行:claimDetailVC.removeFromParentViewController(),这不是从内存中释放当前的视图控制器吗?
  • 不,当它位于选项卡视图控制器中时,您不应该将其删除。只需更改选定的索引。
猜你喜欢
  • 2011-12-25
  • 2017-10-13
  • 2014-03-03
  • 2013-02-15
  • 1970-01-01
  • 2011-12-16
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多