I am working on RxSwift and started creating few basic. I have added new button however with rx_tap subscribe not working for button action. Below is my code, please let me know what I am doing wrong
let button = UIButton(frame: CGRect(x: 10, y: 66, width: 100, height: 21))
button.backgroundColor = UIColor.redColor()
button.setTitle("Login", forState: UIControlState.Normal)
let disposeBag = DisposeBag()
button.rx_tap
.subscribe { [weak self] x in
self!.view.backgroundColor = UIColor.redColor()
}
.addDisposableTo(disposeBag)
self.view.addSubview(button)
Your subscription is cancelled immediately because of the scope of your DisposeBag
. It is created, then goes out of scope and immediately deallocated. You need to retain your bag somewhere. If you are using a view controller or something like that, you can create a property there and assign it to that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.