Skip to content

Instantly share code, notes, and snippets.

@vknabel
Last active August 31, 2015 08:25
Show Gist options
  • Save vknabel/3cd107df48f48923adc9 to your computer and use it in GitHub Desktop.
Save vknabel/3cd107df48f48923adc9 to your computer and use it in GitHub Desktop.
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
}
}
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