ReactiveSwift - 将“Action”绑定到“UIControl”
ReactiveSwift - Bind `Action` to `UIControl`
我正在尝试将 Action
绑定到 UISwitch
。
我使用以下代码创建了操作
action = Action<UISwitch, Bool, NoError> { (input: UISwitch) -> SignalProducer<Bool, NoError> in
return SignalProducer{ (observer, disposable) in
observer.send(value: input.isOn)
observer.sendCompleted()
}
}
但我无法将它连接到 UISwitch
。
有人可以帮忙吗?
还有另一个 class CocoaAction
用于包装 Action
s 并将它们连接到 UIControl
s(现在在 ReactiveCocoa
而不是核心 ReactiveSwift
,因此如果您使用的是 RAC 5,则必须同时导入两者)
var switch: UISwitch!
//switch.addTarget() does not retain the target, so if we do not
//keep a strong reference here the cocoaAction will be deallocated
//at the end of viewDidLoad() and you will get unrecognized selector
//errors when the switch tries to execute the action
var switchCocoaAction: CocoaAction!
override func viewDidLoad() {
let action = Action<UISwitch, Bool, NoError> { (input: UISwitch) -> SignalProducer<Bool, NoError> in
return SignalProducer { (observer, disposable) in
observer.send(value: input.isOn)
observer.sendCompleted()
}
}
//unsafe because it will cast anyObject as! UISwitch
self.switchCocoaAction = action.unsafeCocoaAction
switch.addTarget(switchCocoaAction,
action: CocoaAction.selector,
forControlEvents: .ValueChanged
)
}
但是,如果您想要的只是一个信号,只要它发生变化就会发出 switch.isOn
值,您可以使用内置 rac_signalForControlEvents
更轻松地完成此操作
func switchSignal() -> SignalProducer<Bool, NoError> {
switch.rac_signalForControlEvents(.ValueChanged) //returns legacy RACsignal
.toSignalProducer() //legacy RACSignal -> swift SignalProducer
.flatMapError { _ in .empty } //NSError -> NoError, errors not possible here, so ignore them
.map { ([=11=] as! UISwitch).isOn }
}
我正在尝试将 Action
绑定到 UISwitch
。
我使用以下代码创建了操作
action = Action<UISwitch, Bool, NoError> { (input: UISwitch) -> SignalProducer<Bool, NoError> in
return SignalProducer{ (observer, disposable) in
observer.send(value: input.isOn)
observer.sendCompleted()
}
}
但我无法将它连接到 UISwitch
。
有人可以帮忙吗?
还有另一个 class CocoaAction
用于包装 Action
s 并将它们连接到 UIControl
s(现在在 ReactiveCocoa
而不是核心 ReactiveSwift
,因此如果您使用的是 RAC 5,则必须同时导入两者)
var switch: UISwitch!
//switch.addTarget() does not retain the target, so if we do not
//keep a strong reference here the cocoaAction will be deallocated
//at the end of viewDidLoad() and you will get unrecognized selector
//errors when the switch tries to execute the action
var switchCocoaAction: CocoaAction!
override func viewDidLoad() {
let action = Action<UISwitch, Bool, NoError> { (input: UISwitch) -> SignalProducer<Bool, NoError> in
return SignalProducer { (observer, disposable) in
observer.send(value: input.isOn)
observer.sendCompleted()
}
}
//unsafe because it will cast anyObject as! UISwitch
self.switchCocoaAction = action.unsafeCocoaAction
switch.addTarget(switchCocoaAction,
action: CocoaAction.selector,
forControlEvents: .ValueChanged
)
}
但是,如果您想要的只是一个信号,只要它发生变化就会发出 switch.isOn
值,您可以使用内置 rac_signalForControlEvents
func switchSignal() -> SignalProducer<Bool, NoError> {
switch.rac_signalForControlEvents(.ValueChanged) //returns legacy RACsignal
.toSignalProducer() //legacy RACSignal -> swift SignalProducer
.flatMapError { _ in .empty } //NSError -> NoError, errors not possible here, so ignore them
.map { ([=11=] as! UISwitch).isOn }
}