【发布时间】:2019-01-19 19:39:02
【问题描述】:
我有委托方法,我需要在 RxSwift 中由委托代理包装。我已经使用 Bond 和 Reactive 完成了它,但是在这里,在 RxSwift 中,我无法找到转换它的正确方法。
遵循协议
import UIKit
/**
A protocol for the delegate of a `DetailInputTextField`.
*/
@objc
public protocol CardInfoTextFieldDelegate {
/**
Called whenever valid information was entered into `textField`.
- parameter textField: The text field whose information was updated and is valid.
- parameter didEnterValidInfo: The valid information that was entered into `textField`.
*/
func textField(_ textField: UITextField, didEnterValidInfo: String)
/**
Called whenever partially valid information was entered into `textField`.
- parameter textField: The text field whose information was updated and is partially valid.
- parameter didEnterPartiallyValidInfo: The partially valid information that was entered.
*/
func textField(_ textField: UITextField, didEnterPartiallyValidInfo: String)
/**
Called whenever more text was entered into `textField` than necessary. This can be used to provide this overflow as text in the next text field in the responder chain.
- parameter textField: The text field which received more information than required.
- parameter overFlowDigits: The overflow of text which does not fit into `textField` and might be entered into the next receiver in the responder chain.
*/
func textField(_ textField: UITextField, didEnterOverflowInfo overFlowDigits: String)
}
我之前做的是
import Foundation
import Bond
import Caishen
extension DetailInputTextField {
var bnd_cardInfoDelegate: ProtocolProxy {
return protocolProxy(for: CardInfoTextFieldDelegate.self, setter: NSSelectorFromString("setCardInfoTextFieldDelegate:"))
}
var bnd_didEnterValidInfo: StreamSignal<NSString> {
return bnd_cardInfoDelegate.signal(for: #selector(CardInfoTextFieldDelegate.textField(_:didEnterValidInfo:)))
{ (s: PublishSignal<NSString>, _: UITextField, info: NSString) in
s.next(info)
}
}
var bnd_didEnterPartiallyValidInfo: StreamSignal<NSString> {
return bnd_cardInfoDelegate.signal(for: #selector(CardInfoTextFieldDelegate.textField(_:didEnterPartiallyValidInfo:)))
{ (s: PublishSignal<NSString>, _: UITextField, info: NSString) in
s.next(info)
}
}
var bnd_didEnterOverflowInfo: StreamSignal<NSString> {
return bnd_cardInfoDelegate.signal(for: #selector(CardInfoTextFieldDelegate.textField(_:didEnterOverflowInfo:)))
{ (s: PublishSignal<NSString>, _: UITextField, info: NSString) in
s.next(info)
}
}
}
如何在 RxSwift 中做同样的练习。 我尝试了 DelegateProxy,但不清楚它是如何正确包装它的。
【问题讨论】:
-
如果你使用的是 RxSwift(观察),为什么还要继续使用委托?
-
那么,在哪里调用或分配某个类的委托方法?
-
如果你观察 Q,有四个委托,并且需要在主控制器中通过包装使用它们,但是我们如何在 Rx 中做同样的事情?
标签: swift rx-swift delegation