skip to Main Content

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 ?

3

Answers


  1. 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

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
  3. It sounds like you’re describing a scenario where you have an enum representing different states, and based on the state, you’re rendering different views. Specifically, when the state is .loaded, you’re showing self.showDetails, which is a view controller, and it works fine when a cell is selected from a table view.

    Here’s an example of how you might handle this scenario in Swift:

    enum ViewState {
        case loaded
        // Add other states as needed
    }
    
    class MainViewController: UIViewController {
        var currentState: ViewState = .loaded // Assume initial state is loaded
        
        // Function to handle cell selection in table view
        func didSelectCell() {
            switch currentState {
            case .loaded:
                // Show details view controller
                let detailsViewController = self.showDetails
                // Present details view controller or navigate to it as needed
                // For example:
                self.navigationController?.pushViewController(detailsViewController, animated: true)
            // Handle other states as needed
            }
        }
        
        // Function to get the details view controller
        var showDetails: UIViewController {
            // Instantiate and configure details view controller
            let detailsVC = DetailsViewController()
            // Configure details view controller if needed
            return detailsVC
        }
    }
    
    class DetailsViewController: UIViewController {
        // Details view controller implementation
    }

    We have an enum ViewState representing different states.
    In MainViewController, there’s a currentState property to keep track of the current state.

    There’s a didSelectCell() function to handle cell selection in the table view. It switches on the current state and performs appropriate actions.

    In the .loaded case, it retrieves the details view controller using self.showDetails and presents it.

    The showDetails property is a computed property that instantiates and configures the details view controller as needed.

    DetailsViewController is a placeholder for your actual details view controller implementation.

    You would need to adapt this example to fit your specific implementation details, such as how you handle navigation and how you configure the details view controller.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search