Latest 1.1.0
Homepage https://github.com/drewolbrich/SeguePerformer
License MIT
Platforms ios 11.0
Frameworks UIKit
Authors

Travis
Platform
Swift 5
License
Twitter

Overview

The SeguePerformer class provides an interface for initiating UIKit segues programmatically, using closures for view controller preparation.

Background

In UIKit’s existing programmatic segue presentation interface, UIViewController’s performSegue(withIdentifier:sender:) method relies on a non-local definition of prepare(for:sender:) to configure the new view controller before it is presented. This can become unwieldy in the context of multiple performSegue calls.

SeguePerformer improves upon this by providing a performSegue(withIdentifier:sender:preparationHandler:) method which allows for configuration of the new view controller via a trailing closure parameter.

Installation

To install SeguePerformer using CocoaPods, add this line to your Podfile:

pod 'SeguePerformer'

To install using Carthage, add this to your Cartfile:

github "drewolbrich/SeguePerformer"

Or, if you prefer, drag the file SeguePerformer.swift into your project.

Usage

  1. Add a lazy seguePerformer property to your view controller.
  2. Call seguePerformer.performSegue(withIdentifier:sender:preparationHandler:) to initiate segues whose destination view controllers require configuration.
  3. Override your view controller’s prepare(for:sender:) method, passing its parameters along to seguePerformer.prepare(for:sender:). Without this step, the preparationHandler closure will never be called.

Example

import SeguePerformer

class MyPresentingViewController: UIViewController {

    lazy var seguePerformer = SeguePerformer(viewController: self)

    func performMySegue(with myPropertyValue: Int) {
        // Perform the segue, configuring the destination view controller
        // before it is presented.
        seguePerformer.performSegue(withIdentifier: "mySegue", sender: self) {
            (myPresentedViewController: MyPresentedViewController) in
            myPresentedViewController.myProperty = myPropertyValue
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // For SeguePerformer.performSegue's trailing closure to be called,
        // SeguePerformer must be notified about the prepare(for:sender:) call.
        if seguePerformer.prepare(for: segue, sender: sender) {
            return
        }

        // Prepare for interactive segues configured in Storyboard here.
    }

}

Without SeguePerformer, the traditional way of writing this would be:

class MyPresentingViewController: UIViewController {

    var myPresentedViewControllerPropertyValue: Int?

    func performMySegue(with myPropertyValue: Int) {
        myPresentedViewControllerPropertyValue = myPropertyValue
        performSegue(withIdentifier: "mySegue", sender: self)
        // Continues in prepare(for:sender:)...
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let myPresentedViewController = segue.destination as? MyPresentedViewController,
            let myPresentedViewControllerPropertyValue = myPresentedViewControllerPropertyValue {
            // ...continued from performMySegue(with:)
            myPresentedViewController.myProperty = myPresentedViewControllerPropertyValue
            self.myPresentedViewControllerPropertyValue = nil
        }
    }

}

License

This project is licensed under the terms of the MIT open source license. Please refer to the file LICENSE for the full terms.

Latest podspec

{
    "name": "SeguePerformer",
    "version": "1.1.0",
    "summary": "A Swift class for initiating segues programmatically, using closures for view controller preparation",
    "description": "A downside of UIViewController's performSegue(withIdentifier:sender:) is thatnconfiguration of the presented view controller must occur separately innprepare(for:sender:) instead of locally at the call site. Thisncan become particularly awkward in the context of multiple segues.nSeguePerformer improves upon this by providingnpeformSegue(withIdentifier:sender:preparationHandler:), which allows fornconfiguration of the new view controller via a trailing closure parameter.",
    "homepage": "https://github.com/drewolbrich/SeguePerformer",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "drewolbrich": "[email protected]"
    },
    "source": {
        "git": "https://github.com/drewolbrich/SeguePerformer.git",
        "tag": "1.1.0"
    },
    "social_media_url": "https://twitter.com/drewolbrich",
    "platforms": {
        "ios": "11.0"
    },
    "source_files": "Source/**/*.swift",
    "frameworks": "UIKit",
    "swift_versions": [
        "4.2",
        "5.0"
    ]
}

Pin It on Pinterest

Share This