Skip to content

Instantly share code, notes, and snippets.

@anelad
Created January 15, 2018 20:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anelad/6e4827a1f0b384e65bcec36caa143948 to your computer and use it in GitHub Desktop.
Save anelad/6e4827a1f0b384e65bcec36caa143948 to your computer and use it in GitHub Desktop.
Swift 4 implementation for checking if segue with identifier exists.
import UIKit
extension UIViewController {
/**
Checks whether controller can perform specific segue or not.
- parameter identifier: Identifier of UIStoryboardSegue.
*/
func canPerformSegue(withIdentifier identifier: String) -> Bool {
//first fetch segue templates set in storyboard.
guard let identifiers = value(forKey: "storyboardSegueTemplates") as? [NSObject] else {
//if cannot fetch, return false
return false
}
//check every object in segue templates, if it has a value for key _identifier equals your identifier.
let canPerform = identifiers.contains { (object) -> Bool in
if let id = object.value(forKey: "_identifier") as? String {
if id == identifier{
return true
} else {
return false
}
} else {
return false
}
}
return canPerform
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment