apple/UIKit & ReactiveX
[iOS] RxAction
lgvv
2022. 3. 15. 23:33
RxAction
✅ Rx를 공부하다가 RxAction을 발견해서 한번 공부해 보기로 함
https://github.com/RxSwiftCommunity/Action
✅ [APP UI]
APP UI를 확인해보기!!
✅ ActionViewController.swift
//
// ActionViewController.swift
// RxDataSource
//
// Created by 이건우 on 2022/01/18.
//
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")
}
}
코드를 보면 쉽게 이해할 수 있을거야