swiftxcodeuiviewcontrollerswiftuinscoder

Missing argument for parameter 'coder' in call, in UIViewControllerRepresentable struct


I'm trying to wrap a UITabBarController in a SwiftUI view using UIViewControllerRepresentable but ran into a problem that I haven't been able to solve for days. I've looked through every single possible thread and have tried all proposed solutions with no luck. Any help would be tremendously appreciated.

TabViewController.swift

import UIKit
import ResearchKit
import CareKit
import SwiftUI

struct TabBarView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UITabBarController {
        let tabBarViewController = TabBarViewController()
        return tabBarViewController
    }
    
    func updateUIViewController(_ uiViewController: UITabBarController, context: Context) {
        
    }
    
    typealias UIViewControllerType = UITabBarController
    
    
}

class TabBarViewController: UITabBarController {
  fileprivate let carePlanStoreManager = CarePlanStoreManager.sharedCarePlanStoreManager
  fileprivate let carePlanData: CarePlanData
  fileprivate var symptomTrackerViewController: OCKSymptomTrackerViewController? = nil
  fileprivate var insightsViewController: OCKInsightsViewController? = nil
  fileprivate var insightChart: OCKBarChart? = nil
  
  required init?(coder aDecoder: NSCoder) {
    carePlanData = CarePlanData(carePlanStore: carePlanStoreManager.store)

    super.init(coder: aDecoder)

    carePlanStoreManager.delegate = self
    carePlanStoreManager.updateInsights()

    let careCardStack = createCareCardStack()
    let symptomTrackerStack = createSymptomTrackerStack()
    let insightsStack = createInsightsStack()
    let connectStack = createConnectStack()
    
    self.viewControllers = [careCardStack,
                            symptomTrackerStack,
                            insightsStack,
                            connectStack]
    
    tabBar.tintColor = UIColor.systemTeal
    tabBar.barTintColor = UIColor.white
  }

I've also attached a screenshot that displays the exact error.enter image description here


Solution

  • Try to move everything into init as following (you don't need coder in this case)

    class TabBarViewController: UITabBarController {
        fileprivate let carePlanData: CarePlanData
        fileprivate var symptomTrackerViewController: OCKSymptomTrackerViewController? = nil
        fileprivate var insightsViewController: OCKInsightsViewController? = nil
        fileprivate var insightChart: OCKBarChart? = nil
        
        fileprivate let carePlanStoreManager = CarePlanStoreManager.sharedCarePlanStoreManager
        init() {
            carePlanData = CarePlanData(carePlanStore: carePlanStoreManager.store)
            
            super.init(nibName: nil, bundle: nil)
            
            carePlanStoreManager.delegate = self
            carePlanStoreManager.updateInsights()
            
            let careCardStack = createCareCardStack()
            let symptomTrackerStack = createSymptomTrackerStack()
            let insightsStack = createInsightsStack()
            let connectStack = createConnectStack()
            
            self.viewControllers = [careCardStack,
                                            symptomTrackerStack,
                                            insightsStack,
                                            connectStack]
            
            tabBar.tintColor = UIColor.systemTeal
            tabBar.barTintColor = UIColor.white
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }