user6398734
user6398734

Reputation:

Bind action in uibutton -Reactive Cocoa

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?

Upvotes: 4

Views: 2458

Answers (1)

MeXx
MeXx

Reputation: 3357

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"
}

Upvotes: 6

Related Questions