Skip to content

Instantly share code, notes, and snippets.

@asmallteapot
Created September 12, 2015 03:56
Show Gist options
  • Save asmallteapot/579d69412bb86e1f127f to your computer and use it in GitHub Desktop.
Save asmallteapot/579d69412bb86e1f127f to your computer and use it in GitHub Desktop.
Handling storyboard segues with enumerations in Swift
class MonolithViewController: UIViewController {
enum InnerSegueType {
case WebView
case Inspector
static func match(segue: UIStoryboardSegue, _ sender: AnyObject?) -> InnerSegueType? {
if segue.identifier == "WebView" {
return .WebView
}
if segue.identifier == "Inspector" {
return .Inspector
}
return nil
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let segueType = InnerSegueType.match(segue, sender) else {
return
}
switch segueType {
case .WebView:
print("configure web view controller")
case .Inspector:
print("configure inspector")
}
}
}
@erynofwales
Copy link

Are you still looking for feedback on this? You could make the enum back by a String and use its constructor to get the enum value.

enum InnerSegueType: String {
    case WebView = "WebView
    case Inspector = "Inspector"

    static func match(segue: UIStoryboardSegue, _ sender: AnyObject?) -> InnerSegueType? {
        return InnerSegueType(segue.identifier)
    }
}

Something like that maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment