【问题标题】:Why does shouldPerformSegue use a string, and how do I use a UIStoryboardSegue with it instead?为什么 shouldPerformSegue 使用字符串,以及如何使用 UIStoryboardSegue 代替它?
【发布时间】:2018-11-02 18:16:34
【问题描述】:

例如,使用 prepare(for segue:.. 我可以简单地传递 segue 值:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let nav = segue.destination as? UINavigationController, let manageCaptureVC = nav.topViewController as? ManageCaptureVC {

    }
}

但是现在我想在它被触发时选择性地取消它,而且似乎我只能用shouldPerformSegue 来做到这一点,因为在准备中使用 return(for segue:.. 不会停止任何事情。

shouldPerformSegue 使用字符串而不是 UIStoryboardSegue。我不确定为什么会这样,我想要UIStoryboardSegue 值。

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if debug_tutorialAllowCaptureBtnActions == false {
        return false
    }
    //how do I get segue?
    if let nav = segue.destination as? UINavigationController, let manageCaptureVC = nav.topViewController as? ManageCaptureVC {

    }

    return true
}

【问题讨论】:

  • 我想我只是误解了 shouldPerformSegue 的作用。相反,我使用这两个函数并且不在 shouldPerformSegue 中导航,只检查 debug_tutorialAllowCaptureBtnActions 的值并返回。这可以防止 prepare(for segue: 运行。

标签: ios swift segue uistoryboardsegue


【解决方案1】:

你需要

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
   if identifier == "segueName" {   
      return
    }
 }

获取 segue 本身是没有意义的。您只需要知道 segue 标识符。此外,如果您需要做出此决定,请替换

if debug_tutorialAllowCaptureBtnActions == false {
    return false
}

return tutorialAllowCaptureBtnActions 

【讨论】:

  • 你的例子应该说“return false”,因为shouldPerformSegue返回一个布尔值。
猜你喜欢
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
相关资源
最近更新 更多