【问题标题】:Swift How to present view in root navigation after dismiss modalSwift 如何在关闭模式后在根导航中呈现视图
【发布时间】:2016-11-09 07:41:20
【问题描述】:

我正在尝试在 Modal 关闭后让 ViewController 出现

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let chatRoomVC = storyboard.instantiateViewController(withIdentifier: "ChatRoomVCId") as! ChatRoomVC
chatRoomVC.hidesBottomBarWhenPushed = true
chatRoomVC.passValue = passValue

self.dismiss(animated: true, completion: {
    self.present(chatRoomVC, animated: true, completion: nil)
})

但它会返回“谁的视图不在窗口层次结构中!”也许它在控制器被解雇后呈现一个视图

【问题讨论】:

标签: ios swift uiviewcontroller uinavigationcontroller


【解决方案1】:

注意self.present中的self,你在做什么,基本上是告诉vc你正在解雇展示一个新的vc,这是错误的做法,正确的方法是告诉它是PARENT vc来展示一个new vc,通过delegate/unwind调用父vc来呈现新的vc

【讨论】:

  • 我使用 unwind 来进行操作。但是我收到了另一个错误“开始/结束外观转换的不平衡调用”,即使我禁用了展开 segue 的动画。我在 unwind IBAction 中调用 performSegue。
  • 您是否在self.dismiss(animated: true, completion: 中调用了 unwind?
  • 我只是打电话给self.performSegue(withIdentifier: "unwindToVC", sender: self.passValue)。无需使用解除功能即可工作。如果我将它添加到关闭完成中,它不会生效
  • 啊,是的,这是因为它也会处理解除操作,在你的 unwind 方法中,将 performSegue 放入 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { ... } 以延迟它一点,然后警告就会消失
【解决方案2】:

你正在使用自我。这意味着您正在关闭当前的视图控制器。应该是父视图控制器将呈现一个新的视图控制器。

【讨论】:

    【解决方案3】:

    当从“Controller_A”呈现到“Controller_B”时->如下所示呈现它

    --- 查看控制器 A ---

    self.navigationController?.present(Controller_B, animated: true, completion: nil)
    

    当您想关闭“Controller_B”并使用导航控制器呈现“Controller_C”时

    --- 视图控制器 B ---

    let presenController : UINavigationController = self.presentingViewController as! UINavigationController
    
    presentingController.dismiss(animated: true, completion: {
      presenController.pushViewController(Controller_C, animated: true)
    })
    

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 1970-01-01
      • 2020-05-30
      • 2013-10-28
      • 1970-01-01
      • 2016-10-12
      • 2015-04-26
      • 2012-05-02
      • 1970-01-01
      相关资源
      最近更新 更多