[英]RxSwift MVVM implemention inquire
I have two UITextField
and one UITableView
in one UIViewController
. 我在一个
UIViewController
有两个UITextField
和一个UITableView
。 The ViewModel of this UIViewController
exposes one Observable to be the table view datasouce. 此
UIViewController
的ViewModel公开一个Observable作为表视图数据源。 The two UITextField
take the parameters from users and refresh the table view Observable. 两个
UITextField
从用户那里获取参数并刷新表视图Observable。 UITextField
are using UIPickerView
as InputView and those UIPickerViews
are using a custom class as the adapter. UITextField
使用UIPickerView
作为InputView,而那些UIPickerViews
使用自定义类作为适配器。
In UIViewController: 在UIViewController中:
//bind the adapter to the UIPickerView
self.viewModel.dateList.bind(to: self.datePickerView.rx.items(adapter: self.viewModel.pickerViewAdapter)).disposed(by: rx.disposeBag)
In ViewModel: 在ViewModel中:
let dateList = Observable.just([[Int](0...1), [Int](1...12)])
final class PickerViewViewAdapter
: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType
, SectionedViewDataSourceType {
typealias Element = [[Int]]
private var items: [[Int]] = []
var activeTextField:UITextField!
func model(at indexPath: IndexPath) throws -> Any {
return items[indexPath.section][indexPath.row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel()
if component == 0{
label.text = "\(2018 - items[component][row])"
}else{
label.text = "\(items[component][row])"
}
label.font = UIFont.preferredFont(forTextStyle: .title1)
label.textAlignment = .center
return label
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
///
/// Need to update the UITextField.text in a specific form
///
}
func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
Binder(self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}
After the user select from UIPickerView
I need to update the UITextField.text
in a specific form, and then use those text as the parameters for the API. 用户从
UIPickerView
选择后,我需要以特定形式更新UITextField.text
,然后将这些文本用作API的参数。
How should I do this in a Reactive way in MVVM? 我应该如何在MVVM中以响应方式执行此操作?
I'm not sure about what all is happening in your code, but maybe this will help out: 我不确定您的代码中正在发生什么,但是这可能会有所帮助:
let items = Observable.just(Calendar.current.monthSymbols)
let pickerView = UIPickerView()
textField.inputView = pickerView
// this pipe displays the items in the picker view
items
.bind(to: pickerView.rx.itemTitles) { $1 }
.disposed(by: bag)
// this pipe displays the picked item in the text field
Observable.combineLatest(items, pickerView.rx.itemSelected) { $0[$1.row] }
.bind(to: textField.rx.text)
.disposed(by: bag)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.