簡體   English   中英

使用ReactiveCocoa 4和NSButton計數豆

[英]Counting beans with ReactiveCocoa 4 and an NSButton

我有以下幾點:

  • 兩個有趣的類: ViewControllerViewModel
  • ViewController view的一個按鈕nsButtonMorePlease:NSButton
  • view中的文本框nsTextView:NSTextView也是如此

我想要以下行為:

  • 啟動程序時,“ count”從0開始,並顯示在nsTextView文本框中nsTextView
  • 當您按下按鈕nsButtonMorePlease ,計數將增加1 ,更新的計數將反映在nsTextView

我想確保:

  • 我使用ReactiveCocoa 4 (這就是重點)
  • 該模型類包含numberOfBeans: MutableProperty<Int>0開始
  • 該設計純粹是功能性的或接近它的-即(如果我理解該術語),鏈中的每個鏈接都將鼠標單擊事件映射到numberOfBeansMutableProperty ,以在文本視圖中對其做出響應,所有這些都沒有副作用。

這就是我所擁有的。 合理的警告:我認為這接近工作或編譯。 但是我確實感覺好像我想使用combineLatestcollectreduce 。只是迷失了要做什么。 我確實覺得這使事情變得很困難。

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