【问题标题】:Binding ViewModel to ViewController (ReactiveCocoa) iOS将 ViewModel 绑定到 ViewController (ReactiveCocoa) iOS
【发布时间】:2016-04-25 11:37:45
【问题描述】:

我现在正在实现简单的 facebook/google 登录。但是尝试在我的项目中将 MVVM 模式与 ReactiveCocoa 一起应用。我无法将 viewModel 与 viewControllers 绑定。尝试了 CocoaActions 但无法使其工作。

查看模型:

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

var fbLoginAction:Action<OnboardingViewController,Bool,NSError>

视图控制器:

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

【问题讨论】:

    标签: ios swift mvvm reactive-cocoa


    【解决方案1】:

    这个问题发布已经有一段时间了。从那时起,RAC 5.0.0 已经发布,这使得这变得更加容易,这要归功于 UI 绑定:

    假设您已经准备好视图模型中定义的fbLoginAction,您可以将此操作绑定到视图控制器中的按钮,如下所示:

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

    您最初的问题可能是因为您提供了() 作为输入,但您的fbLoginAction 的输入定义为OnboardingViewController

    取决于您是否真的打算这样做并且需要OnboardingViewController 作为fbLoginAction 的输入,您可以按照上面发布的input: self 进行操作,或者如果您实际上不需要输入,您可以改成

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 2011-12-17
      相关资源
      最近更新 更多