Latest 0.0.1
License MIT
Platforms ios 8.3, tvos 9.1

Better segue handling in Swift



gem "segue_handler_plugin"


plugin 'segue_handler_plugin'
pod 'WillowTreeSegueHandler'

Build Phases

Add a run script phase before the "Compile Sources" phase, with the following:

bundle exec pod --no-ansi --silent generate-segue-handlers $PROJECT_DIR $PROJECT_NAME

Build it once, then add the SegueExtensions.swift file to your project and
build again.

Note: Depending on your local Ruby setup, you may need to install the plugin
outside of the management of Bundler.


Segues with identifiers will be pulled out into nested enums in extensions on
your view controllers, written to SegueExtensions.swift.

Example generated code, assuming A and B are view controllers connected
with from A to B via the segue with identifier "ToB".

extension A: SegueHandler {
    enum SegueIdentifier: String {
        case ToB

    enum ToSegueDestination: SegueDestination {
        case ToB(B)

        init?(identifier: SegueIdentifier, destination: UIViewController) {
            switch identifier {
            case .ToB:
                guard let vc = destination as? B else { return nil }
                self = ToB(vc)

These can be used in your view controller like so:

func pushManually() {
    performSegue(.ToB, sender: self)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segueFromStoryboardSegue(segue) {
    case .ToB(let b):
        b.prepare(dependency: "injected from a")

See the enclosed Example application for a working demo.


This is an extension of the approach outlined at WWDC and written up by
Natasha the Robot

Specifically, it adds the casting to desired view controller type in the extension,
and adds code generation for the enums.

WillowTree is Hiring!

Want to write amazing tvOS apps? Want to write amazing iOS apps?
Check out our openings!

Latest podspec

    "name": "WillowTreeSegueHandler",
    "version": "0.0.1",
    "summary": "Better segue handling in Swift",
    "description": "Use protocol extensions with enumerated values to makenyour segue handling nicer and more type safe for dependencyninjection.",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "Ian Terrell": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.0.1"
    "platforms": {
        "ios": "8.3",
        "tvos": "9.1"
    "source_files": [
    "public_header_files": "SegueHandler/**/*.h"

Pin It on Pinterest

Share This