我正在探索Rx,我想看看在初始化此元素的组件之前,是否可以声明性地相互定义某些UI元素的输入和输出.
这就是它的样子:
class ViewController: UIViewController { @IBOutlet weak var button: UIButton! var child: ButtonComponent! override func viewDidLoad() { super.viewDidLoad() let buttonInput = PublishSubject<()>() let buttonOutput = buttonInput .scan(0) { count, _ in count + 1 } .map { $0.description } self.child = ButtonComponent( button: self.button, input: buttonInput, output: buttonOutput ) } } class ButtonComponent { weak var button: UIButton! let disposeBag = DisposeBag() init(button: UIButton, input: PublishSubject<()>, output: Observable) { self.button = button output .subscribeNext { string in button.setTitle(string, forState: .Normal) } .addDisposableTo(disposeBag) button.rx_tap .subscribeNext { input.onNext($0) } .addDisposableTo(disposeBag) } }
这里的问题是我正在使用PublishSubject将更新推送到输入流,这是必要的并且建议不要使用.Rx是否提供了以某种方式将序列添加到已运行的observable的方法?