Skip to content

Instantly share code, notes, and snippets.

@haldun
Created September 23, 2015 07:20
Show Gist options
  • Save haldun/fab59bbb2334f10852ff to your computer and use it in GitHub Desktop.
Save haldun/fab59bbb2334f10852ff to your computer and use it in GitHub Desktop.
SegueHandlerType
//
// SegueHandlerType.swift
//
import UIKit
protocol SegueHandlerType {
typealias SegueIdentifier: RawRepresentable
}
extension SegueHandlerType where Self: UIViewController, SegueIdentifier.RawValue == String {
func performSegueWithIdentifier(segueIdentifier: SegueIdentifier, sender: AnyObject?) {
performSegueWithIdentifier(segueIdentifier.rawValue, sender: sender)
}
func segueIdentifierForSegue(segue: UIStoryboardSegue) -> SegueIdentifier {
guard let identifier = segue.identifier,
segueIdentifier = SegueIdentifier(rawValue: identifier)
else { fatalError("Invalid segue identifier \(segue.identifier)") }
return segueIdentifier
}
}
//
// ViewController.swift
//
import UIKit
class ViewController: UIViewController, SegueHandlerType {
enum SegueIdentifier: String {
case ShowDetail = "ShowDetail"
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segueIdentifierForSegue(segue) {
case .ShowDetail:
print("Configure next view controller here")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment