我正在处理这种情况,我想在这里应用RxSwift。
我有.xib
UIView按钮。
class RightButtonItemView: UIView {
@IBOutlet weak var rightButtonimageView: UIImageView!
@IBOutlet weak var rightButtonButton: UIButton!
let performEventSegue = PublishSubject<Bool>()
class func instanceFromNib() -> RightButtonItemView {
return UINib(nibName: "NotificationView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! RightButtonItemView
}
override func awakeFromNib() {
//I'm not sure how should I link button tap and my subject
rightButtonButton.rx.tap
}
}
我在viewController中创建了这个视图的实例
var rightButtonItemView = RightButtonItemView.instanceFromNib()
从我的segue点击这个按钮我需要转到下一个VC,所以我订阅了UIView的主题
rightButtonItemView.performEventSegue.asObserver().subscribe(onNext: { (isAuthorized) in
if isAuthorized {
let storyboard = UIStoryboard(name: "Notifications", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "UserNotificationsViewController")
self.present(viewController, animated: true, completion: nil)
}
})
但我无法理解如何将按钮点按与我的发布主题的状态更改?
答案 0 :(得分:3)
在这种情况下使用bind
运算符:
class RightButtonItemView: UIView {
@IBOutlet weak var rightButtonimageView: UIImageView!
@IBOutlet weak var rightButtonButton: UIButton!
let performEventSegue = PublishSubject<Void>()
private let bag = DisposeBag()
class func instanceFromNib() -> RightButtonItemView {
return UINib(nibName: "NotificationView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! RightButtonItemView
}
override func awakeFromNib() {
rightButtonButton.rx.tap
.bind(to: performEventSegue)
.disposed(by: bag)
}
}
- 更新 -
我想到了这个解决方案,我觉得它更好。您根本不需要主题:
class RightButtonItemView: UIView {
@IBOutlet weak var rightButtonButton: UIButton!
var performEventSegue: Observable<Void> {
return rightButtonButton.rx.tap.asObservable()
}
}