Skip to content

Instantly share code, notes, and snippets.

@TheCodedSelf
Created May 13, 2017 17:16
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save TheCodedSelf/b1ad83927b470f32aad8d4fde415327e to your computer and use it in GitHub Desktop.
Save TheCodedSelf/b1ad83927b470f32aad8d4fde415327e to your computer and use it in GitHub Desktop.
RxSwift: Bind to button taps
import UIKit
import RxSwift
import RxCocoa
let button = UIButton()
button.rx.tap.bind {
print("button tapped")
}
@Aquima
Copy link

Aquima commented May 21, 2018

cool!

@kjoelbro-zz
Copy link

Simple and cool. But I think the disposed-by is missing:

button.rx.tap
    .bind {
        print("button tapped")
    }
    .disposed(by: someDisposeBag)

@luucassoares
Copy link

Dont know the difference but Im used to do this way:

btnSend.rx.tap.subscribe(onNext: { [ weak self ] in
self?.showLoader()
self?.viewModel.updatePassword(success: {
self?.hideLoader()
self?.navigationController?.popToRootViewController(animated: true)
}) { (message, highlight) in
self?.hideLoader()
self?.showErrorAlert(message: message)
}
}).disposed(by: viewModel.bag)

@imjn
Copy link

imjn commented Mar 22, 2019

Which is the better way?

@oleksandr17
Copy link

Using bind is better way because tap is a ControlEvent<()>, so it never emits an error. In case of using subscribe, it's not obvious anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment