A lightweight replacement for target action with closures, modified from Scream.swift.
In UIKit, Target-Action has been the default way to handle control events until the arrival of iOS 8 when UIAlertController introduces closure handler in UIAlertAction.
Closure handlers, in many cases, are more concise and readable than Target-Action. ReactiveUI follows this approach, wrapping existing Target-Action APIs
// UIControl
func addTarget(_ target: AnyObject?, action action: Selector, forControlEvents controlEvents: UIControlEvents)
// UIGestureRecognizer
init(target target: AnyObject, action action: Selector)
// ...
in closures
// UIControl
func addAction(action: UIControl -> (), forControlEvents events: UIControlEvents)
// UIGestureRecognizer
init(action: UIGestureRecognizer -> ())
// ...
With ReactiveUI, control events handling is much simpler:
var button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
button.setTitle("Title", forState: .Normal)
button.addAction({_ in println("TouchDown")}, forControlEvents: .TouchDown)
button.addAction({_ in println("TouchUpInside")}, forControlEvents: .TouchUpInside)
button.addAction({_ in println("TouchDragOutside")}, forControlEvents: .TouchDragOutside)
You can install ReactiveUI
through CocoaPods adding the following to your Podfile:
pod 'ReactiveUI'
CocoaPods' support for swift is still pre-released, and requires your iOS deployment target to be 8.0 or later:
[sudo] gem install cocoapods --pre
You can install ReactiveUI
through Carthage adding the following to your Cartfile:
github 'zhxnlai/ReactiveUI'
Checkout the demo app for an example.
ReactiveUI currently supports the following classes:
###UIControl
init(action: UIControl -> (), forControlEvents events: UIControlEvents)
init(forControlEvents events: UIControlEvents, action: UIControl -> ())
func addAction(action: UIControl -> (), forControlEvents events: UIControlEvents)
// can be called with a trailing closure
func forControlEvents(events: UIControlEvents, addAction action: UIControl -> ())
func removeAction(forControlEvents events: UIControlEvents)
func actionForControlEvent(events: UIControlEvents) -> (UIControl -> ())?
var actions: [UIControl -> ()]
###UIBarButtonItem
init(barButtonSystemItem systemItem: UIBarButtonSystemItem, action: UIBarButtonItem -> ())
init(title: String?, style: UIBarButtonItemStyle, action: UIBarButtonItem -> ())
init(image: UIImage?, style: UIBarButtonItemStyle, action: UIBarButtonItem -> ())
init(image: UIImage?, landscapeImagePhone: UIImage?, style: UIBarButtonItemStyle, action: UIBarButtonItem -> ())
func addAction(action: UIBarButtonItem -> ())
func removeAction()
###UIGestureRecognizer
init(action: UIGestureRecognizer -> ())
func addAction(action: UIGestureRecognizer -> ())
func removeAction()
###NSTimer
class func scheduledTimerWithTimeInterval(seconds: NSTimeInterval, action: NSTimer -> (), repeats: Bool) -> NSTimer
ReactiveUI is available under MIT license. See the LICENSE file for more info.