4

I have declared a action as

var postAction: Action<String, Data, NSError>!

now what i want is to trigger this action when the button triggers.

 triggerBtn.reactive.pressed = CocoaAction(postAction)

But can't.How can i trigger some action when a button is pressed using reactive cocoa?

I have figured out a way to observe the actions.

self.testBtn.reactive.trigger(for: .touchUpInside).observe{ event in
        //do something
        print(event)

    }

But cant figure out how to get the sender and bind Custom Action?

1 Answers1

6

Your Action has an input of type String, but the default CocoaAction is no input (type ()).

Depending on where your input should come from when the button is pressed, you can use either a fixed value (if its known at this point):

button.reactive.pressed = CocoaAction(postAction, input: "Some Input")

or an inputTransformer

button.reactive.pressed = CocoaAction(postAction) { sender in
    // Calculate input for the action when the button is pressed
    return "Some Value"
}
MeXx
  • 3,357
  • 24
  • 39
  • Can you say me how do i get the sender in this code please `self.pressBtn.reactive.trigger(for: .touchDown)...get sender` –  Dec 01 '16 at 17:30
  • Well, the sender is just the button thats being pressed. Only thing to keep in mind is to capture the button weakly as to not create a strong reference cycle: `button.reactive.trigger(for: .touchDown).observeValues { [weak button = button] in print("Sender: \(button)") }` – MeXx Dec 01 '16 at 18:13