Search code examples
iosswiftsegueuistoryboardsegue

Why does shouldPerformSegue use a string, and how do I use a UIStoryboardSegue with it instead?


For instance, with prepare(for segue:.. I could simply pass the segue value:

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

    }
}

But now I want to optionally cancel it if it's triggered, and it only seems I can do that with shouldPerformSegue, since using return in prepare(for segue:.. does not stop anything.

shouldPerformSegue uses a String instead of UIStoryboardSegue. I'm not sure why this is, and I'd like to have the UIStoryboardSegue value.

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
}

Solution

  • You need

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

    Getting the segue itself is meaningless. You only need to know the segue identifier. Also, if you need to make this the decision then replace

    if debug_tutorialAllowCaptureBtnActions == false {
        return false
    }
    

    with

    return tutorialAllowCaptureBtnActions