rea

ReactiveUI

A lightweight replacement for target action with closures, modified from Scream.swift.

Showing:

Popularity

Downloads/wk

0

GitHub Stars

52

Maintenance

Last Commit

6yrs ago

Contributors

2

Package

Dependencies

0

License

MIT

Categories

Readme

Reactive UI

A lightweight replacement for target action with closures, modified from Scream.swift.

Why?

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)

CocoaPods

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

Carthage

You can install ReactiveUI through Carthage adding the following to your Cartfile:

github 'zhxnlai/ReactiveUI'

Usage

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

License

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100