Skip to content

Commit 708d56f

Browse files
Merge pull request #74 from stefanomondino/documentation
Documentation for bindTo
2 parents 2767d96 + 22d7c8b commit 708d56f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Readme.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ Now when the button is pressed, the action is executed. The button's `enabled` s
5858

5959
If you'd like to use `Action` to do a complex operation such as file download with download progress report (to update progress bar in the UI for example) you'd use `Action<Void, Int>` instead of `CocoaAction`. Out of the box `CocoaAction` can't emit progress values, your own `Action<Void, Int>` will do that. For details refer to [this article](http://www.sm-cloud.com/rxswift-action/).
6060

61+
If your scenario involves many buttons that needs to trigger the same `Action` providing different input, you can use `bindTo` on each `UIButton` with a closure that returns correct input.
62+
63+
```swift
64+
let button1 = UIButton()
65+
let button2 = UIButton()
66+
67+
let action = Action<String,String> { input in
68+
print(input)
69+
return .just(input)
70+
}
71+
button1.rx.bindTo(action) {_ in return "Hello"}
72+
button2.rx.bindTo(action) {_ in return "Goodbye"}
73+
```
74+
75+
`button1` and `button2` are sharing the same `Action`, but they are feeding it with different input (`Hello` and `Goodbye` that will be printed for corresponding tap).
76+
77+
A more complex use case can be a single action related to a `UIViewController` that manages your navigation, error handling and loading state. With this approach, you can have as many `UIButton`s (or `UIBarButtonItem`s) as you want and subscribe to `executing`, `errors` and `elements` once and in a single common place.
78+
6179
There's also a really cool extension on `UIAlertAction`, used by [`UIAlertController`](http://ashfurrow.com/blog/uialertviewcontroller-example/). One catch: because of the limitations of that class, you can't instantiate it with the normal initializer. Instead, call this class method:
6280

6381
```swift
@@ -88,7 +106,7 @@ github "RxSwiftCommunity/Action" ~> 2.1.1
88106
then run
89107

90108
```
91-
$ carthage update
109+
$ carthage update
92110
```
93111

94112
Thanks

0 commit comments

Comments
 (0)