iOS RxAction (RxSwift Community)
RxSwift를 사용하면서 모든 챕터를 다 알아두면 좋을 것 같아서 보게 되었음.
이거 좀 유용할 것 같다는 생각이 든달까?
RxSwift Communtiry에 Pinned도 되어있음
깃헙 위치
- https://github.com/RxSwiftCommunity/Action
샘플 예시
아주 간단한 예시이고, RefreshControl에서도 사용할 수 있음
RxCommunity - Action 부분에서 Tests를 가보면 다른 예시도 볼 수 있으니까 궁금하면 확인해보기!!
import UIKit
import RxCocoa
import RxSwift
import SnapKit
import Then
import Action
class ActionViewController: UIViewController {
var bag = DisposeBag()
var button1 = UIButton().then {
$0.setTitle("button1", for: .normal)
$0.backgroundColor = .brown
}
let button2 = UIButton().then {
$0.setTitle("button2", for: .normal)
$0.backgroundColor = .cyan
}
var button3 = UIButton().then {
$0.setTitle("button3", for: .normal)
$0.backgroundColor = .link
}
let action1: Action<Void ,Void> = Action {
print("Doing some work")
return Observable.empty()
}
let action2 = Action<String ,String> { input in
print("action2 \(input)")
return .just(input)
}
lazy var action3 = Action<String ,Void> { input in
let alert = UIAlertController(title: input, message: input, preferredStyle: .alert)
let okAction = UIAlertAction.Action("OK", style: .default)
let cancelAction = UIAlertAction.Action("Cancel", style: .destructive)
[okAction, cancelAction].forEach { alert.addAction($0) }
self.present(alert, animated: true, completion: nil)
return Observable.empty()
}
override func viewDidLoad() {
super.viewDidLoad()
[button1, button2, button3].forEach {
view.addSubview($0)
}
button1.snp.makeConstraints {
$0.top.leading.trailing.equalTo(view.safeAreaLayoutGuide)
}
button2.snp.makeConstraints {
$0.top.equalTo(button1.snp.bottom)
$0.leading.trailing.equalToSuperview()
}
button3.snp.makeConstraints {
$0.top.equalTo(button2.snp.bottom)
$0.leading.trailing.equalToSuperview()
}
bindAction()
}
func bindAction() {
button1.rx.action = action1
button2.rx.bind(to: action2, input: "say Hello?")
button3.rx.bind(to: action3, input: "Button3")
}
}
'apple > RxSwift, ReactorKit' 카테고리의 다른 글
[ReactorKit] ReactorKit 공부하기 #1 (0) | 2022.07.24 |
---|---|
RxSwift Signal,Emit (0) | 2022.02.19 |
RxSwift ch 18. Table & Collection views (0) | 2022.01.18 |
Extension Reactive (RxSwift) (1) | 2022.01.12 |
iOS RxDelegateProxy 만들어보기 #1 (0) | 2022.01.12 |