반응형
ViewModel
var currentSelectedCategoryIdx = PublishSubject<Int>()
- 초기값이 필요없는 경우 PublishSubject로 ViewModel에 property 정의
struct Input {
// 생략
}
struct Output {
let changedCategoryIdx: Observable<Int>
}
- viewController에서 값을 받아 사용해야 하므로 Output에 반환할 Observable 타입을 정의해줌
func transform(input: Input) -> Output {
let changedCategoryIdx = currentSelectedCategoryIdx.asObserver()
return Output(changedCategoryIdx: changedCategoryIdx)
}
- transform함수에서 Observable로 반환
self?.currentSelectedCategoryIdx.onNext(categoryId)
- viewModel에서 값을 방출하고 싶은 경우 onNext로 전달
viewController
private func setupBindings() {
let input = QuoteEditorViewModel.Input()
output.changedCategoryIdx
.bind { [weak self] idx in
// 생략
}
.disposed(by: disposeBag)
}
- viewModel에서 정의해 준 observable을 바인딩 해서 사용함
728x90
반응형
'🍎 iOS' 카테고리의 다른 글
[iOS/Swift] XCTestCase 생성하기 (0) | 2023.04.13 |
---|---|
[iOS/Swift] Unit Test 란? (0) | 2023.04.10 |
[iOS/Swift] UICollectionView Dynamic Height with AutoConstraint (0) | 2023.03.25 |
[iOS/Xcode] Pod file not found 이슈 (0) | 2023.03.23 |
[Swift] FSCalendar 커스텀 캘린더 정리 (0) | 2023.03.16 |
댓글