Latest 2.0.0
Homepage https://github.com/ukitaka/Stimulator
License MIT
Platforms ios 9.0, requires ARC
Authors

Version
License
Platform

Carthage compatible
Swift Package Manager

Custom event handling based on Responder Chain.

Usage

Create custom event and handler protocol

import Stimulator

struct ShowAlertEvent : Stimulator.Event {

    typealias Responder = ShowAlertResponder

        let title: String
        let message: String

        init(_ title: String, _ message: String) {
            self.title = title
                self.message = message
        }

    func stimulate(responder: Responder) {
        responder.showAlert(event: self)
    }
}

protocol ShowAlertResponder {

    func showAlert(event: ShowAlertEvent)

}

Generate event

Generates event in UIResponer subclass (e.g. UIView, UIViewController)

self.stimulate(event: ShowAlertEvent("title", "message"))

Handle event

class MyViewController: UIViewController, ShowAlertResponder {

    func showAlert(event: ShowAlertEvent) {
        let alert = UIAlertController(title: event.title, message: event.message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { _ in }))
        self.show(alert, sender: nil)
    }

}

You can use protocol extensions to provide a default implementation.

extension ShowAlertResponder where Self : UIViewController {

    func showActionSheet(event: ShowActionSheetEvent) {
        let alert = UIAlertController(title: event.title, message: event.message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { _ in }))
        self.showViewController(alert, sender: nil)
    }

}

Requirements

  • iOS 9.0+
  • Xcode8+

Installation

Stimulator is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "Stimulator"

Author

yuki.takahashi, [email protected]

License

Stimulator is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Stimulator",
    "version": "2.0.0",
    "summary": "Custom event handling based on Responder Chain",
    "description": "StimulatornCustom event handling based on Responder Chain",
    "homepage": "https://github.com/ukitaka/Stimulator",
    "license": "MIT",
    "authors": {
        "yuki.takahashi": "[email protected]"
    },
    "source": {
        "git": "https://github.com/ukitaka/Stimulator.git",
        "tag": "2.0.0"
    },
    "platforms": {
        "ios": "9.0"
    },
    "requires_arc": true,
    "source_files": "Sources/Stimulator.swift",
    "pushed_with_swift_version": "3.0.2"
}

Pin It on Pinterest

Share This