coreDeviOS
coreDeviOS

Reputation: 1538

Binding ViewModel to ViewController (ReactiveCocoa) iOS

i am implementing simple facebook/google login for now. But trying to apply MVVM pattern with ReactiveCocoa in my project. I am not able to bind the viewModel with viewControllers . Tried CocoaActions but not able to make it work.

View Model :

let name = MutableProperty<String>("")
let email = MutableProperty<String>("")
let phoneNo = MutableProperty<String>("")
let referal = MutableProperty<String>("")

var fbLoginAction:Action<OnboardingViewController,Bool,NSError>

View Controller :

        //MARK: Signup Binding
    let loginCocoaAction = CocoaAction(viewModel.fbLoginAction., input:())
    signupView.fbBtn.addTarget(loginCocoaAction, action: CocoaAction.selector, forControlEvents: .TouchUpInside)

Upvotes: 0

Views: 704

Answers (1)

MeXx
MeXx

Reputation: 3357

Its been a while since this question was posted. Since then, RAC 5.0.0 has been released which makes this a lot easier, thanks to UI Bindings:

Assuming, you have fbLoginAction ready as defined in the View Model, you can bind this action to a button in the View Controller like this:

signupView.fbBtn.reactive.pressed = CocoaAction(fbLoginAction, input: self)

Your original problem was probably because you provided () as input, but the input of your fbLoginAction is defined as OnboardingViewController.

Depends on whether you actually meant to do that and need the OnboardingViewController as input to the fbLoginAction, you can do it as posted above with input: self, or if you don't actually need the input, you can change it to

let fbLoginAction:Action<(),Bool,NSError>

and

signupView.fbBtn.reactive.pressed = CocoaAction(fbLoginAction)

Upvotes: 1

Related Questions