Skip to content

Instantly share code, notes, and snippets.

@alexj70
Last active April 25, 2017 17:38
Show Gist options
  • Save alexj70/29b245c19092e78d0902d1bb38922bf5 to your computer and use it in GitHub Desktop.
Save alexj70/29b245c19092e78d0902d1bb38922bf5 to your computer and use it in GitHub Desktop.

Storyboard segue

import UIKit
protocol SegueHandler {
    associatedtype SegueIdentifier: RawRepresentable
}

extension SegueHandler where Self: UIViewController, SegueIdentifier.RawValue == String {
    
    func performSegue(withIdentifier identifier: SegueIdentifier, sender: Any?) {
        performSegue(withIdentifier: identifier.rawValue, sender: sender)
    }
    
    func shouldPerformSegue(withIdentifier identifier: SegueIdentifier, sender: Any?) -> Bool {
        return shouldPerformSegue(withIdentifier: identifier.rawValue, sender: sender)
    }
    
    func segueIdentifier(for segue: UIStoryboardSegue) -> SegueIdentifier {
        guard let identifier = segue.identifier, let segueIdentifier = SegueIdentifier(rawValue: identifier) else {
            fatalError("Invalid segue identiifer \(segue.identifier ?? "NaN")")
        }
        return segueIdentifier
    }
    
    func segueIdentifier(for identifier: String?) -> SegueIdentifier {
        guard let str = identifier, let segueIdentifier = SegueIdentifier(rawValue: str) else {
            fatalError("Invalid segue identiifer \(identifier ?? "NaN")")
        }
        return segueIdentifier
    }
    
}

NibLoadedView

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