【问题标题】:How do I push a view controller in one case, but present it modally in another?如何在一种情况下推送视图控制器,但在另一种情况下以模态方式呈现它?
【发布时间】:2019-10-07 20:32:48
【问题描述】:

我有一个视图控制器 (VC3),如果通过一个屏幕 (VC1) 访问我想 push,但如果从另一个屏幕访问它模态 ( VC2)。目标视图控制器 (VC3) 位于其自己的故事板上,并嵌入在 UINavigationController 中(我认为这在这里非常重要)。

在 VC1 上的 prepare(for segue) 我有这个:

 if let nav = segue.destination as? UINavigationController {
     let destinationViewController = nav.topViewController as! LiftsViewController
     destinationViewController.delegate = self
   }

这以模态方式呈现目标视图控制器 (VC3),这正是我想要的。

在 VC2 中,将通过点击表格视图中的单元格来访问视图控制器 (VC3),因此我希望它推送。但是在 VC2 上的 prepare(for segue) 中,我有与上面相同的代码,因为,好吧,我正在使用 UINavigationController 中的同一个视图控制器。

我已经查看了很多解决推送 vcs 或以模态方式呈现它们的线程,但找不到任何可以回答如何使用同一个 vc 进行这两种操作的内容。

谢谢。

【问题讨论】:

  • 您不能将NavigationController 推送到另一个NavigationController
  • @MojtabaHosseini,但这实际上并没有帮助。

标签: swift segue uistoryboardsegue


【解决方案1】:

在这种情况下,我们不要使用 segue 并以编程方式推送/展示您的 VC。

let vc3 = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LiftsViewController") as? LiftsViewController)!
vc3.delegate = self

现在推送:

self.navigationController?.pushViewController(vc3, animated: true)

或现在:

self.presentViewController(vc3, animated: true, completion: nil)

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多