uptechteam / Coordinator-MVVM-Rx-Example

Example of MVVM-C architecture implemented with RxSwift
564 stars 100 forks source link

Using coordinator to embed a controllers into subviews #11

Open chaaarly opened 5 years ago

chaaarly commented 5 years ago

Hello,

i started using coordinators in my app. It's ok to present controllers throught navigation controllers, tabbar controllers or modally. But i wonder how to use it to embed a view controller in a subview. My app has a 2 panels controller and each panel contains its own sub view controller (with coordinator).

I tried this way :

`

class MainCoordinator: BaseCoordinator {

private let navigationController: UINavigationController

init(navigationController: UINavigationController) {
    self.navigationController = navigationController
}

override func start() -> Observable<Void> {
    let viewController = MainViewController.initFromStoryboard(name: "Main")

    let viewModel = MainViewModel()
    viewController.viewModel = viewModel

    self.navigationController.pushViewController(viewController, animated: true)

    self.presentFirstPanel(intoContainerView: viewController.firstPanelViewContainer)

    return Observable.never()
}

private func presentFirstPanel(intoContainerView containerView:UIView) {
    let firstPanelCoordinator = FirstPanelCoordinator(withContainerView: containerView)
    _ = self.coordinate(to: organizationPanelCoordinator)
}

}

class FirstPanelCoordinator: BaseCoordinator {

private let containerView:UIView
init(withContainerView containerView:UIView) {
    self.containerView = containerView
}

override func start() -> Observable<Void> {

    let viewController = FirstPanelViewController.initFromStoryboard(name: "OrganizationPanel")

    let viewModel = FirstPanelViewModel()
    viewController.viewModel = viewModel

    self.containerView.addSubview(viewController.view)
    viewController.view.frame = self.containerView.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    return Observable.never()
}

}`

... but when i add my first panel to the container view, the container view is nil so it crash.