[英]Counting beans with ReactiveCocoa 4 and an NSButton
我有以下几点:
ViewController
和ViewModel
ViewController
view
的一个按钮nsButtonMorePlease:NSButton
view
中的文本框nsTextView:NSTextView
也是如此 我想要以下行为:
nsTextView
文本框中nsTextView
nsButtonMorePlease
,计数将增加1
,更新的计数将反映在nsTextView
我想确保:
ReactiveCocoa 4
(这就是重点) numberOfBeans: MutableProperty<Int>
从0
开始 numberOfBeans
的MutableProperty
,以在文本视图中对其做出响应,所有这些都没有副作用。 这就是我所拥有的。 合理的警告:我认为这接近工作或编译。 但是我确实感觉好像我想使用combineLatest
, collect
, reduce
。只是迷失了要做什么。 我确实觉得这使事情变得很困难。
class CandyViewModel {
private let racPropertyBeansCount: MutableProperty<Int> = MutableProperty<Int>(0)
lazy var racActionIncrementBeansCount: Action<AnyObject?, Int, NoError> = {
return Action { _ in SignalProducer<Int, NoError>(value: 1)
}
}()
var racCocoaIncrementBeansAction: CocoaAction
init() {
racCocoaIncrementBeansAction = CocoaAction.init(racActionIncrementBeansCount, input: "")
// ???
var sig = racPropertyBeansCount.producer.combineLatestWith(racActionIncrementBeansCount.)
}
}
class CandyView: NSViewController {
@IBOutlet private var guiButtonMoreCandy: NSButton!
@IBOutlet private var guiTextViewCandyCt: NSTextView!
}
class CandyViewModel {
let racPropertyBeansCount = MutableProperty<Int>(0)
let racActionIncrementBeansCount = Action<(), Int, NoError>{ _ in SignalProducer(value: 1) }
init() {
// reduce the value from the action to the mutableproperty
racPropertyBeansCount <~ racActionIncrementBeansCount.values.reduce(racPropertyBeansCount.value) { $0 + $1 }
}
}
class CandyView: NSViewController {
// define outlets
let viewModel = CandyViewModel()
func bindObservers() {
// bind the Action to the button
guiButtonMoreCandy.addTarget(viewModel.racActionIncrementBeansCount.unsafeCocoaAction, action: CocoaAction.selector, forControlEvents: .TouchUpInside)
// observe the producer of the mutableproperty
viewModel.racPropertyBeansCount.producer.startWithNext {
self.guiTextViewCandyCt.text = "\($0)"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.