简体   繁体   English

使用ReactiveCocoa 4和NSButton计数豆

[英]Counting beans with ReactiveCocoa 4 and an NSButton

I have the following: 我有以下几点:

  • Two interesting classes: a ViewController and a ViewModel 两个有趣的类: ViewControllerViewModel
  • A button nsButtonMorePlease:NSButton in the view of ViewController ViewController view的一个按钮nsButtonMorePlease:NSButton
  • A text box nsTextView:NSTextView in the view as well view中的文本框nsTextView:NSTextView也是如此

I want the following behavior: 我想要以下行为:

  • When you launch the program, the "count" starts at 0 and is displayed in the text box nsTextView 启动程序时,“ count”从0开始,并显示在nsTextView文本框中nsTextView
  • When you press the button nsButtonMorePlease , the count is incremented by 1 and the updated count is reflected in the nsTextView 当您按下按钮nsButtonMorePlease ,计数将增加1 ,更新的计数将反映在nsTextView

I would like to ensure: 我想确保:

  • I use ReactiveCocoa 4 (that's the point kind of) 我使用ReactiveCocoa 4 (这就是重点)
  • The model class contains numberOfBeans: MutableProperty<Int> starting at 0 该模型类包含numberOfBeans: MutableProperty<Int>0开始
  • The design is purely functional or close to it - that is (if I understand the term), every link in the chain mapping the event of mouse click to the MutableProperty of numberOfBeans to responding to it in the text view, is all without side effects. 该设计纯粹是功能性的或接近它的-即(如果我理解该术语),链中的每个链接都将鼠标单击事件映射到numberOfBeansMutableProperty ,以在文本视图中对其做出响应,所有这些都没有副作用。

Here's what I have. 这就是我所拥有的。 Fair warning: this doesn't come close to working or compiling I believe. 合理的警告:我认为这接近工作或编译。 But I do feel like maybe I want to use one of combineLatest , collect , reduce , etc. Just lost on what to do specifically. 但是我确实感觉好像我想使用combineLatestcollectreduce 。只是迷失了要做什么。 I do feel like this makes something easy quite hard. 我确实觉得这使事情变得很困难。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM