0

我有以下内容:

  • 两个有趣的类:aViewController和 aViewModel
  • nsButtonMorePlease:NSButtonview的一个按钮ViewController
  • nsTextView:NSTextView还有一个文本view

我想要以下行为:

  • 启动程序时,“计数”从 0 开始并显示在文本框中nsTextView
  • 当您按下按钮nsButtonMorePlease时,计数会增加 1,1更新后的计数会反映在nsTextView

我想确保:

  • 我用ReactiveCocoa 4(这就是重点)
  • 模型类包含numberOfBeans: MutableProperty<Int>开始于0
  • 该设计纯粹是功能性的或接近于它-也就是说(如果我理解该术语),链中的每个链接都将鼠标单击事件映射到文本视图MutablePropertynumberOfBeans响应它的事件,都没有副作用。

这就是我所拥有的。公平警告:我相信这并不接近工作或编译。但我确实觉得也许我想使用 , , 等中的一个combineLatestcollect只是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!



}
4

1 回答 1

1
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)"
        }

    }

}
于 2015-12-02T10:21:02.153 回答