How to get which button is clicked in RxSwift

rxswift button tap
rxswift button tap in cell
rxswift controlevent
rxswift textfield
uitapgesturerecognizer rxswift
rxswift uilabel
rxswift bind vs subscribe
rxcocoa bind button tap

I have created a common action for an array of my button. I just want to get the which button is tapped.

I have array of buttons like let buttons = [UIButton(), UIButton(), UIButton(),UIButton()].

let observable = Observable.of(buttons[0].rx.tap, buttons[1].rx.tap, buttons[2].rx.tap, buttons[3].rx.tap).merge()
    observable.subscribe(onNext: {
      print("I want to find which button is tapped.")
    }).disposed(by: disposeBag)

The correct answer is to not merge the buttons in the first place. If you want to do four different things, then have four different observables. If they are all doing the same thing, just with different data then simply:

let taps = buttons.enumerated().map { ($0.0, $0.1.rx.tap) }
let toInts = taps.map { index, obs in obs.map { index } }
let mergedTaps = Observable.merge(toInts)

On review, I really like an answer by @Sooraj_snr that has been deleted. Use the buttons' tags instead of their position in the array. It's much more robust.

let tags = buttons
    .map { ($0.rx.tap, $0.tag) }
    .map { obs, tag in obs.map { tag } }
let values = Observable.merge(tags)

import RxSwift. import RxCocoa. let button = UIButton(). button.rx.tap.bind {. print(​"button tapped"). } Copy link. @Aquima. Aquima commented about 2 years ago. And that will allow the login button to be active when the e-mail and password inputs are true and when clicked, for them to go to menu page. Let’s add the code below to the bottom of the

You are using merge(). You can't know which on is being tapped.

If you want multiple buttons but one action you are doing the right thing.

let observable = Observable.of(buttons[0].rx.tap, buttons[1].rx.tap, 
    buttons[2].rx.tap, buttons[3].rx.tap).merge()
    observable.subscribe(onNext: {
    print("I want to find which button is tapped.")
    }).disposed(by: disposeBag)

If the action is different then: Example Let's say I have a UIView and 4 buttons. In button tap, you change the background of the view. Different color per button. So same function but different color.

Observable.of(UIButton().rx.tap.map { _ in UIColor.red }).merge().subscribe(onNext: { color in
       UIView().backgroundColor = color
   })

how can i handle action of multiple button by combining them in RxSwift? o.​subscribe(onNext: { //tag of button that is clicked }) I have five buttons in VC and four buttons maintains isSelected property if either of the four  Tweet. Intro. Common misconception is that you cannot have state with Rx. Well you can - and there’s a special operator that helps you to: scan. If you’ve ever used reduce in Swift - scan is a very similar operator but reduce goes over the complete sequence and gives you the final value of the accumulator while scan emits each intermediate value as well.

Just map the tap events to some custom IDs -

let observable = Observable.merge(
  buttons[0].rx.tap.map { 0 },
  buttons[1].rx.tap.map { 1 },
  // etc.
)

observable.subscribe(onNext: { id in
  print("\(id) button is tapped.")
}).disposed(by: disposeBag)

A new request will be sent every time button is pressed. You can get required streams of UI elements only after view was loaded and outlets  A user can tap on a button in the navigation bar to show the second screen. On the languages screen, he can select a language or dismiss the screen by tapping on the cancel button. If a user selects a language the screen will dismiss and the repositories list will update according to the selected language.

Here we give the button tag.

buttons.enumerated().forEach { (index, button) in
  button.tag = index
}

Here we got the which button we have selected.

for button in buttons {
  button.rx.tap.subscribe { [weak self] event in
    print("Selected Button :- \(button.tag)")
    }.disposed(by: disposeBag)
}

Install the 'RxSwift' and 'RxCocoa' pods. (Don't forget to close the project and open it again from the .xcworkspace file); Now go to the Project  2. Implementation of binding. Get row, element, cell and make some operation with them to init TableViewCells. RxSwift and RxCocoa greatly facilitate the code writing process. In common programming this project will take many lines of code and need initialization of delgate functions and a lot of things that avoided with ReactiveX.

You can add style to the buttons to make it easier to see. I'm going to Click on "​Show the Assistant Editor Button" so that Main.storyboard and  So you have some custom view. In your ViewModel, there’s some data stream that comes in. And then you’re going to bind to the sink. Therefore you have the ability to get a UIView to accept Reactive streams. And the way to get it is, using CocoaPods or Carthage, RxSwift and RxCocoa.

Thanks to it you don't have to keep a strong reference to the button.rx.tap observable in above example. However, you have to break the retain  I am new to RxSwift and RxCocoa and I'm learning it. I want to validate all textfield on button click and based on the validation I need to show alert message to user. After validation is successful I need to insert record in table. Refer the following code

For that you'll need to install RxSwift and RxCocoa, so add them to your In our initState function we retrieve a Stream of button pressed from  nullify dispose bag to get rid of previous subscriptions; bind enabled state of the Action to rx_enabled of the button so that it can be clicked only when appropriate action.enabled.bindTo(self.rx_enabled).addDisposableTo(self.actionDisposeBag) add rx_tap observer to call execute() on the button when it's tapped.

Comments
  • Please write this as comment, not as an answer.
  • @tspentzas, Can you please suggest another way instead of using merge().
  • This is a great answer but the way you have it setup requires a whole lot of boilerplate. Better would be to have an array of colors along with the array of buttons, zip them and then combine them like I did in my answer. The result would be shorter code that's easer to understand and change (adding and removing buttons would be much less painful.) I'm upvoting your answer anyway, but I would love to see you upgrade it by incorporating some of my answer.