文字绑定到 UILabel
let label = UILabel()
// ...
/// 值绑定
Observable.just("Hello RxSwift")
.bind(to: label.rx.text)
.disposed(by: disposeBag)
监听单个 textField 内容的变化
方式一:
let textField = UITextField()
// ...
textField.rx.text.orEmpty.asObservable().subscribe(onNext: {
print("你输入的是: \($0)")
}).disposed(by: disposeBag)
方式二:
let textField = UITextField()
// ...
textField.rx.text.orEmpty.changed.subscribe(onNext: {
print("你输入的是: \($0)")
}).disposed(by: disposeBag)
监听多个 textField 内容的变化
let inputField1 = UITextField()
let inputField2 = UITextField()
// ...
Observable.combineLatest(inputField1.rx.text.orEmpty, inputField2.rx.text.orEmpty) {
(textValue1, textValue2) in
print("你输入的号码是: \(textValue1)-\(textValue2)")
}
将内容绑定到其他控件上
Binder 方式实现
let inputField = UITextField()
let outputField = UITextField()
let label = UILabel()
let button = UIButton()
// ...
// UITextField --> UITextField
inputField.rx.text
.bind(to: outputField.rx.text)
.disposed(by: disposeBag)
// UITextField --> UILabel
inputField.rx.text
.bind(to: label.rx.text)
.disposed(by: disposeBag)
// UITextField --> UIButton
inputField.rx.text
.map({ $0.count > 5 })
.bind(to: button.rx.isEnabled)
.disposed(by: disposeBag)
Driver 方式实现
let inputField = UITextField()
let outputField = UITextField()
let label = UILabel()
let button = UIButton()
// ...
// UITextField --> UITextField
inputField.rx.text.orEmpty.asDriver()
.drive(outputField.rx.text)
.disposed(by: disposeBag)
// UITextField --> UILabel
inputField.rx.text
.drive(label.rx.text)
.disposed(by: disposeBag)
// UITextField --> UIButton
inputField.rx.text.orEmpty
.asDriver()
.map({ $0.count > 5 })
.drive(button.rx.isEnabled)
.disposed(by: disposeBag)
事件监听
通过 rx.controlEvent 可以监听输入框的各种事件,且多个事件状态可以自由组合。除了各种 UI 控件都有的 touch 事件外,输入框还有如下几个独有的事件:
let inputField1 = UITextField()
let inputField2 = UITextField()
// ...
inputField1.rx.controlEvent([.editingDidBegin]).asObservable().subscribe(onNext: {
print("开始编辑")
}).disposed(by: disposeBag)
inputField1.rx.controlEvent([.editingDidEndOnExit]).subscribe(onNext: {
inputField2.becomeFirstResponder()
}).disposed(by: disposeBag)
inputField2.rx.controlEvent([.editingDidEndOnExit]).subscribe(onNext: {
inputField2.resignFirstResponder()
}).disposed(by: disposeBag)
UITextView 包含了 UITextField 的方法
UITextView 独有的方法
UITextView 还封装了如下几个委托回调方法:
let textView = UITextView()
// ...
textView.rx.didBeginEditing.subscribe(onNext: {
print("开始编辑")
}).disposed(by: disposeBag)
textView.rx.didEndEditing.subscribe(onNext: {
print("结束编辑")
}).disposed(by: disposeBag)
textView.rx.didChange.subscribe(onNext: {
print("内容发生改变")
}).disposed(by: disposeBag)
textView.rx.didChangeSelection.subscribe(onNext: {
print("选中部分发生改变")
}).disposed(by: disposeBag)
按钮点击事件的绑定
let button = UIButton()
// ...
button.rx.tap.subscribe(onNext: {
print("我被点击了")
}).disposed(by: disposeBag)
/// 另一种写法
button.rx.tap.bind {
print("我也被点击了")
}.disposed(by: disposeBag)
按钮属性值的绑定
let button = UIButton()
// ...
Observable.just("Click Me")
.bind(to: button.rx.title(for: .normal))
.disposed(by: disposeBag)
Observable.just(UIImage(named: "imageName"))
.bind(to: button.rx.image())
.disposed(by: disposeBag)
let switch1 = UISwitch()
switch1.rx.isOn.bind(to: button.rx.isEnabled)
.disposed(by: disposeBag)
let slider = UISlider()
// ...
slider.rx.value.subscribe(onNext: {
print("当前slider值为: \($0)")
}).disposed(by: disposeBag)
let stepper = UIStepper()
// ...
stepper.rx.value.subscribe(onNext: {
print("当前stepper值为: \($0)")
}).disposed(by: disposeBag)
Slider
与 Stepper
绑定
let slider = UISlider()
let stepper = UIStepper()
// ...
// slider -> stepper
slider.rx.value.map{ Double($0) }
.bind(to: stepper.rx.value)
.disposed(by: disposeBag)
// stepper -> slider
stepper.rx.value.map{ Float($0) }
.bind(to: slider.rx.value)
.disposed(by: disposeBag)
let `switch` = UISwitch()
let button = UIButton()
let activityView = UIActivityIndicatorView()
// ...
`switch`.rx.value
.bind(to: activityView.rx.isAnimating)
.disposed(by: disposeBag)
`switch`.rx.isOn
.bind(to: UIApplication.shared.rx.isNetworkActivityIndicatorVisible)
.disposed(by: disposeBag)
/// 按钮和switch 之间的绑定
`switch`.rx.isOn.bind(to: button.rx.isEnabled).disposed(by: disposeBag)
let segmentControl = UISegmentedControl()
// ...
segmentControl.rx.selectedSegmentIndex.subscribe(onNext: {
print("当前页: \($0)")
}).disposed(by: disposeBag)