TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2017 |
SwiftSwift Version | 3.0.2 |
SPMSupports SPM | ✓ |
Maintained by ukitaka.
Custom event handling based on Responder Chain.
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)
}
Generates event in UIResponer
subclass (e.g. UIView
, UIViewController
)
self.stimulate(event: ShowAlertEvent("title", "message"))
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)
}
}
Stimulator is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Stimulator"
yuki.takahashi, [email protected]
Stimulator is available under the MIT license. See the LICENSE file for more info.