Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication with child view controller #65

Open
jjochen opened this issue Mar 31, 2019 · 2 comments
Open

Communication with child view controller #65

jjochen opened this issue Mar 31, 2019 · 2 comments

Comments

@jjochen
Copy link

jjochen commented Mar 31, 2019

Hello,

I can't wrap my head around how to communicate with a child view controller.

I have one trigger from the ParentViewController that is needed in the ChildViewModel and a second one from the ChildViewModel that should trigger some UI changes in the ParentViewController:

trigger1: ParentViewController (input)> ... (no idea)> ChildViewModel
trigger2: ChildViewModel (output)> ... (no idea)> ParentViewController

My code currently looks something like this:

final class ParentViewController: UIViewController {
    var viewModel: ParentViewModel?
    private let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        bindViewModel()
    }

    private func bindViewModel() {
        guard let viewModel = self.viewModel else {
            fatalError("View Model not set!")
        }
        
        let triggerToChild = rx.someTrigger

        let input = ParentViewModel.Input(triggerToChild: triggerToChild)

        let output = viewModel.transform(input: input)

        output.triggerFromChild
            .drive(rx.someProperty)
            .disposed(by: disposeBag)
    }
}

final class ParentViewModel: ViewModelType {
    struct Input {
       let triggerToChild: Driver<Void>
    }
    struct Output {
       let triggerFromChild: Driver<Void>
    }

    func transform(input: Input) -> Output {
        let triggerFromChild = ??? <===================
        
        return Output(triggerFromChild: triggerFromChild)
    }
}

final class ChildViewController: UIViewController {
    var viewModel: ChildViewModel?
    private let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        bindViewModel()
    }

    private func bindViewModel() {
        guard let viewModel = self.viewModel else {
            fatalError("View Model not set!")
        }

        let triggerFromParent = ??? <===================

        let input = ChildViewModel.Input(triggerFromParent: triggerFromParent)

        let output = viewModel.transform(input: input)

        output.triggerFromParent
            .drive(rx.someProperty)
            .disposed(by: disposeBag)
    }
}

final class ChildViewModel: ViewModelType {
    struct Input {
        let triggerFromParent: Driver<Void>
    }
    struct Output {
        let triggerToParent: Driver<Void>
    }

    func transform(input: Input) -> Output {
        let triggerToParent = rx.someTrigger
        return Output(triggerToParent: triggerToParent)
    }
}

Maybe someone could point me in the right direction? Thank You!

@marktrobinson
Copy link

I had the exact same thought 🤔 how does this ViewModalType work in that scenario?

@marktrobinson
Copy link

This article helps:
https://medium.com/blablacar-tech/rxswift-mvvm-66827b8b3f10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants