showing multiple view controller with table view didselct function

67 Views Asked by At

I am rendering the view based on the enum . For case .loaded , I am showing the self.showDetails which is a view controller and it showing the when I select the cell form table view and it working fine.

case .loaded(let details):
self.showDetails(details)

Here is the code for it ..


 private func showDetails(_ Details: Details) {
        let displayViewController = DetailsDisplayViewController(movieDetails: Details)
        addChild(displayViewController)
        displayViewController.view.frame = view.bounds
        displayViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        currentViewController?.willMove(toParent: nil)
        transition(
            from: currentViewController,
            to: displayViewController,
            duration: 0.25,
            options: [.transitionCrossDissolve],
            animations: nil
        ) { (_) in
            self.currentViewController.removeFromParent()
            self.currentViewController = displayViewController
            self.currentViewController.didMove(toParent: self)
        }
    }

But now I have to add another collection view at the bottom of the details view. I have created another enum for it as well.

case .pageLoaded(let page):
self.showSimiliarDetails(page)
 

Here is the function for it .. same code just reuse it.

private func showSimiliarDetails(_ similiarDetails: Page<Details>) {
        let smiliarViewController = SmiliarController(viewModel: viewModel)
        addChild(smiliarViewController)
        smiliarViewController.view.frame = view.bounds
        smiliarViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        currentViewController?.willMove(toParent: nil)
        transition(
            from: currentViewController,
            to: smiliarViewController,
            duration: 0.25,
            options: [.transitionCrossDissolve],
            animations: nil
        ) { (_) in
            self.currentViewController.removeFromParent()
            self.currentViewController = smiliarViewController
            self.currentViewController.didMove(toParent: self)
        }
    }

I have configured the collection view cell smiliarViewController with data source. I am trying to display both view controller when table view cell is clicked. form table view cell is passing the id and which is get the similar record into collection view .

I have tried to pass it as array and also use + operator like this way ..

let arrayVc = viewController + similiarVC 
self.navigationController?.pushViewController(arrayVc, animated: true)

it gives error .. **Binary operator '+' cannot be applied to operands of type **

the second approach was passing like array ..

let arrayVc = [viewController , similiarVC]
self.navigationController?.pushViewController(arrayVc, animated: true)

but it showing this error ..

Cannot convert value of type '[UIViewController]' to expected argument type 'UIViewController'

Here is the didselcte code ..

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let value = viewModel.state.value[indexPath.row]
            let viewModel = ViewModel(value: value, apiManager: APIManager())
            let viewController = DetailsViewController(viewModel: viewModel)
            let similiarVC = SmiliarViewController(viewModel: viewModel)
            let arrayVc = [viewController , similiarVC]
            self.navigationController?.pushViewController(arrayVc, animated: true)
        }

is there any alternative to do it ?

2

There are 2 best solutions below

0
clawesome On BEST ANSWER

If understand correctly, you want to push both controllers onto the view stack? If so, then you should just be able to call pushViewController(...) twice:

self.navigationController?.pushViewController(viewController, animated: false)
self.navigationController?.pushViewController(similiarVC, animated: true)
1
Andrew Moore On

pushViewController(_:animated:) only accepts a single UIViewController. I believe what you'll need to do is create a container view controller that displays the two view controllers you want to push. Consider this approach: https://developer.apple.com/documentation/uikit/view_controllers/creating_a_custom_container_view_controller