【问题标题】:Navigate to ViewController and dissmiss VC导航到 ViewController 并关闭 VC
【发布时间】:2019-11-25 21:05:25
【问题描述】:

我在这里解雇了一个 VC 并尝试导航新的 VC,但导航不起作用。解雇 VC 工作正常。

@IBAction func onClickEmailBtn(_ sender: UIButton) {
    //dismiss VC
    self.dismiss(animated: false, completion: nil)
    //Navigate to VC
    DispatchQueue.main.async {
        let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
        self.navigationController?.pushViewController(cevc, animated: true)
    }
}

这里我有一个名为 VC1 的 NavigationViewController,我将在上面展示一个名为 VC2 的 VC。在这个 VC2 中,当我单击按钮时,我想导航名为 EmailVC 的新 VC。

【问题讨论】:

  • 能否请您也分享层次结构,当前控制器是否存在?
  • 找出根导航控制器并尝试使用该控制器进行推送。
  • @SGDev,我有一个名为 VC1 的 VC,我正在展示另一个名为 VC2 的 VC,当我在该 VC2 中单击 btn 时,我想导航新的 VC3。在这里,我将 VC2 与 `let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SVC") 作为! SecondViewController //呈现模态 self.present(vc, animated: false, completion: nil)`
  • 尝试在self.dismiss的completion handler中添加pushViewController代码,不用主队列
  • @manishsharma93,你能帮我写代码吗...

标签: ios swift cocoa-touch uiviewcontroller uinavigationcontroller


【解决方案1】:

尝试使用以下代码找出导航控制器并将 Viewcontroller 替换为您的控制器名称。

let productVC = SeconViewViewController()
let parentVC = (parent!.presentingViewController as! ViewController).childViewControllers.first as! UINavigationController
self.navigationController?.dismiss(animated: true, completion: {
     parentVC.pushViewController(productVC, animated: true)
})

【讨论】:

    【解决方案2】:

    试试这个代码

    @IBAction func onClickEmailBtn(_ sender: UIButton) {
    
        if let controller = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController {
             self.dismiss(animated: false, completion: nil)
             self.presentingViewController?.present(controller, animated: true, completion: nil)
        }
    }
    

    【讨论】:

    • 出现错误条件中的模式匹配需要'case'关键字插入大小写以及这里的控制器是什么
    • 解除self后,self在层次结构中不存在,但self.presentingViewController存在。通过使用presentingViewController,我可以推送我的EmailViewController。
    • @SGDev 您可以通过关闭视图控制器来呈现视图控制器,但不能在执行时推送。 OP 的问题是推动 viewController 解除 viewController
    • 你也可以使用presentingViewController进行推送。
    【解决方案3】:

    您试图推动 ViewVontroller 解除视图控制器, 并且您接受的答案是从解除视图控制器中呈现视图控制器。虽然它可能符合你的目的,但我会回答你原来的问题

       // get the object of presenting nanvigation controller(navigation controller with has presented this view controller)
        let presentingNavigationController = presentingViewController?.navigationController
        dismiss(animated: true) {
            let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
            // here you push your view controller with presentingNavigationController
            presentingNavigationController?.pushViewController(cevc, animated: true)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多