Last active
August 31, 2015 08:25
-
-
Save vknabel/3cd107df48f48923adc9 to your computer and use it in GitHub Desktop.
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 SegueHandlerType { | |
typealias SegueIdentifier: RawRepresentable | |
} | |
extension SegueHandlerType where | |
Self: UIViewController, | |
SegueIdentifier.RawValue == String | |
{ | |
//handleSegue with guard | |
// There is a sample project | |
func segueIdentifierForSegue(segue: UIStoryboardSegue) -> SegueIdentifier { | |
guard let identifier = segue.identifier, | |
segueIdentifier = SegueIdentifier(rawValue: identifier) | |
else { fatalError("Unknown identifier \(segue.identifier)") } | |
return segueIdentifier | |
} | |
} |
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 | |
class ViewController: UIViewController, SegueHandlerType { | |
enum SegueIdentifier: String { | |
case FirstSegue = "FirstSegue" | |
case SecondSegue = "SecondSegue" | |
} | |
// In a storyboard-based application, you will often want to do a little preparation before navigation | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
// Get the new view controller using segue.destinationViewController. | |
// Pass the selected object to the new view controller. | |
switch segueIdentifierForSegue(segue) { | |
case .FirstSegue: | |
guard let destination = segue.destinationViewController as? UIViewController | |
else { fatalError("segue not possible") } | |
case .SecondSegue: | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment