Coder Social home page Coder Social logo

segueperformer's Introduction

SeguePerformer

Travis Platforms Swift 4.2 License Twitter

Purpose

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

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 unweildy 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 the following to your Podfile:

pod 'SeguePerformer'

Or, if you prefer, drag 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
        }
    }

}

segueperformer's People

Contributors

drewolbrich avatar

Watchers

Carabineiro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.