Last active
September 11, 2015 03:36
-
-
Save atetlaw/65aacfcc6d80af89fbd3 to your computer and use it in GitHub Desktop.
A silly Swift protocol so you can use an Enum's values to identify storyboard segues
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 | |
protocol SegueNaming: RawRepresentable { | |
static func handle<T : SegueNaming>(segue: UIStoryboardSegue, f: T -> Void) | |
init?(withSegue: UIStoryboardSegue) | |
var segueIdentifier: String { get } | |
} | |
extension SegueNaming where Self.RawValue == String { | |
static func handle<T:SegueNaming>(segue: UIStoryboardSegue, f: T -> Void) { | |
if let segueNameValue = T(withSegue: segue) { | |
f(segueNameValue) | |
} | |
} | |
init?(withSegue: UIStoryboardSegue) { | |
if let identifier = withSegue.identifier { | |
self.init(rawValue: identifier) | |
} else { | |
return nil | |
} | |
} | |
var segueIdentifier: String { | |
return self.rawValue | |
} | |
} | |
extension UIViewController { | |
func performSegueWithSegueName<T:SegueNaming>(segueName: T, sender: AnyObject?) { | |
self.performSegueWithIdentifier(segueName.segueIdentifier, sender: sender) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make an enum
Then you can use it like this in
prepareForSegue(_:)
:Or else you can do this:
there's also a
UIViewController
extension so you can call:self.performSegueWithSegueName(MySegue.ShowDetailViewController, sender: sender)