Created
January 15, 2018 20:26
-
-
Save anelad/6e4827a1f0b384e65bcec36caa143948 to your computer and use it in GitHub Desktop.
Swift 4 implementation for checking if segue with identifier exists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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