gpt4 book ai didi

ios - 无法使用自定义 DelegateProxy 和协议(protocol)接收事件

转载 作者:行者123 更新时间:2023-11-28 15:55:38 25 4
gpt4 key购买 nike

我尝试将 DifficultyViewDelegate 的委托(delegate)迁移到可观察的。这是我的 DifficultyViewDelegate :

@objc protocol DifficultyViewDelegate: class {
func levelDidIncrease()
func levelDidDecrease()
}

还有我的难度 View :

  weak var delegate: DifficultyViewDelegate?

@IBAction func decreaseLevel(_ sender: Any) {
delegate?.levelDidDecrease()
}

@IBAction func increaseLevel(_ sender: Any) {
delegate?.levelDidIncrease()
}

这是我的 RxDifficultyViewDelegateProxy

class RxDifficultyViewDelegateProxy: DelegateProxy, DelegateProxyType {
static func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
let difficultyView: DifficultyView = object as! DifficultyView
return difficultyView.delegate
}

static func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
let difficultyView: DifficultyView = object as! DifficultyView
difficultyView.delegate = delegate as? DifficultyViewDelegate
}
}

我还在我的 DifficultyView 上添加了一个扩展:

extension DifficultyView {
public var rx_delegate: RxDifficultyViewDelegateProxy {
return RxDifficultyViewDelegateProxy.proxyForObject(RxDifficultyViewDelegateProxy.self)
}

public var rx_levelDidIncrease: Observable<Void> {
return rx_delegate.methodInvoked(#selector(DifficultyViewDelegate.levelDidIncrease)).map { _ in return }
}
}

但似乎当我这样做时:

difficultyView.rx_levelDidIncrease.asObservable().subscribe(onNext: {
print("did increase")
}).addDisposableTo(disposeBag)

它从未被调用过。有人有任何指示吗?

最佳答案

尝试使用PublishSubject:

难度 View :

class DifficultyView: UIView {
var levelDidIncrease = PublishSubject<Void>()
var levelDidDecrease = PublishSubject<Void>()

@IBAction func decreaseLevel(_ sender: Any) {
levelDidDecrease.onNext()
}

@IBAction func increaseLevel(_ sender: Any) {
levelDidIncrease.onNext()
}
}

然后:

var difficultyView = DifficultyView()

difficultyView.levelDidDecrease.asObservable()
.subscribe(onNext: {
print("did decrease")
})
.addDisposableTo(disposeBag)


difficultyView.decreaseLevel(theSender) // <- THIS line performs the side effect

关于ios - 无法使用自定义 DelegateProxy 和协议(protocol)接收事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41783083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com