Lucas Huang
Lucas Huang

Reputation: 4016

Binding dose not seem to be working from ReactiveCocoa Swift

var viewModel = CTCViewModel()
var mainView: CTCMainView {
    return self.view as! CTCMainView
}

override func viewDidLoad() {
    super.viewDidLoad()

    let callButtonEnabledSignal = self.viewModel.rac_valuesForKeyPath("callButtonEnabled", observer: self.viewModel)
    callButtonEnabledSignal.setKeyPath("enabled", onObject: self.mainView.callButton, nilValue: false)

    self.mainView.callButton.rac_signalForControlEvents(UIControlEvents.TouchUpInside).subscribeNext {
        (Void) -> Void in
        self.viewModel.callButtonEnabled = !self.viewModel.callButtonEnabled
    }
}

When I press the button, self.viewModel.callButtonEnabled did get updated but the enabled property of the button. It does not seem they are bound.

Upvotes: 0

Views: 47

Answers (1)

Cosyn
Cosyn

Reputation: 5017

Add dynamic on your callButtonEnabled property:

class CTCViewModel: NSObject {
    dynamic var callButtonEnabled = false
}

Because the implementation of rac_valuesForKeyPath is using Objective-C runtime, and the compiler can omit it when access Swift properties. You mark a property with dynamic to let the compiler always use Objective-C runtime.

Upvotes: 2

Related Questions