Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RxSwift,当其他观察对象绑定到按钮时,向按钮添加CoCoAction将产生副作用_Swift_Rx Swift - Fatal编程技术网

RxSwift,当其他观察对象绑定到按钮时,向按钮添加CoCoAction将产生副作用

RxSwift,当其他观察对象绑定到按钮时,向按钮添加CoCoAction将产生副作用,swift,rx-swift,Swift,Rx Swift,我在按钮上添加一个CocoaAction,如 var speakBtn = UIBarButtonItem(title: "Speak", style: .done, target: self, action: nil) speakBtn.rx.action = viewModel.speakAction 之后,将有一个textView的文本绑定到它,如 if let speakBtn = navigationItem.rightBarButtonItems?.first { text

我在按钮上添加一个CocoaAction,如

var speakBtn = UIBarButtonItem(title: "Speak", style: .done, target: self, action: nil)
speakBtn.rx.action = viewModel.speakAction
之后,将有一个textView的文本绑定到它,如

if let speakBtn = navigationItem.rightBarButtonItems?.first {
    textValid.bindTo(speakBtn.rx.isEnabled)
        .addDisposableTo(disposeBag)
}
那么,结果是错误的。如果我对添加cocoaAction到按钮的代码进行注释,当应用程序运行时,textView的文本为零,因此speakBtn将无法单击

但是现在,speakBtn是允许点击的


谁能给我解释一下?谢谢。

我刚找到解决办法

我不应该使用该方法初始化speakAction:

action: Action<String, Bool> = Action(workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action: Action<String, Bool> = Action(enabledIf: validEmailAddress, workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action:action=action(工作工厂:{input in
返回networkLibrary.checkEmailExists(输入)
})
我应该使用该方法初始化speakAction:

action: Action<String, Bool> = Action(workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action: Action<String, Bool> = Action(enabledIf: validEmailAddress, workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action:action=action(enabledIf:validEmailAddress,workFactory:{input in
返回networkLibrary.checkEmailExists(输入)
})
因此,正确的代码应该是:

let textValid = textView.rx.text.orEmpty
    .map{ $0.characters.count > 0 }
    .shareReplay(1)
viewModel.speakAction = CocoaAction(enabledIf: textValid, workFactory: { Void -> Observable<Void> in
    return Observable.create { observer in
        observer.onCompleted()
        return Disposables.create()
    }
})
textValid.bindTo(hintLabel.rx.isHidden)
    .addDisposableTo(disposeBag)
textValid.bindTo(speakBtn.rx.isEnabled)
    .addDisposableTo(disposeBag)
speakBtn.rx.action = viewModel.speakAction
让textValid=textView.rx.text.orEmpty
.map{$0.characters.count>0}
.shareReplay(1)
viewModel.speakAction=CocoaAction(enabledIf:textValid,workFactory:{Void->在
返回可观察的。在中创建{observer
observer.onCompleted()
返回一次性物品。创建()
}
})
textValid.bindTo(hintLabel.rx.ishiden)
.addDisposableTo(disposeBag)
textValid.bindTo(speakBtn.rx.isEnabled)
.addDisposableTo(disposeBag)
speakBtn.rx.action=viewModel.speakAction

我刚刚找到了解决方案

我不应该使用该方法初始化speakAction:

action: Action<String, Bool> = Action(workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action: Action<String, Bool> = Action(enabledIf: validEmailAddress, workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action:action=action(工作工厂:{input in
返回networkLibrary.checkEmailExists(输入)
})
我应该使用该方法初始化speakAction:

action: Action<String, Bool> = Action(workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action: Action<String, Bool> = Action(enabledIf: validEmailAddress, workFactory: { input in
  return networkLibrary.checkEmailExists(input)
})
action:action=action(enabledIf:validEmailAddress,workFactory:{input in
返回networkLibrary.checkEmailExists(输入)
})
因此,正确的代码应该是:

let textValid = textView.rx.text.orEmpty
    .map{ $0.characters.count > 0 }
    .shareReplay(1)
viewModel.speakAction = CocoaAction(enabledIf: textValid, workFactory: { Void -> Observable<Void> in
    return Observable.create { observer in
        observer.onCompleted()
        return Disposables.create()
    }
})
textValid.bindTo(hintLabel.rx.isHidden)
    .addDisposableTo(disposeBag)
textValid.bindTo(speakBtn.rx.isEnabled)
    .addDisposableTo(disposeBag)
speakBtn.rx.action = viewModel.speakAction
让textValid=textView.rx.text.orEmpty
.map{$0.characters.count>0}
.shareReplay(1)
viewModel.speakAction=CocoaAction(enabledIf:textValid,workFactory:{Void->在
返回可观察的。在中创建{observer
observer.onCompleted()
返回一次性物品。创建()
}
})
textValid.bindTo(hintLabel.rx.ishiden)
.addDisposableTo(disposeBag)
textValid.bindTo(speakBtn.rx.isEnabled)
.addDisposableTo(disposeBag)
speakBtn.rx.action=viewModel.speakAction