0

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

2
  • Why are you surprised? You implemented init(coder:) and now that is the only initializer you have. When you say TabBarViewController() you are calling a different initializer, init(), and it doesn't exist. It's as if you had said to me "here, take these blue shoes" and then said "please give me the red shoes". What red shoes? Commented Nov 12, 2020 at 18:19
  • Haha I like the analogy and understand what you mean. I've tried initializing it and inserting all kinds of arguments with no luck. How can I insert the same initializer inside a struct? Commented Nov 12, 2020 at 18:28

1 Answer 1

1

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")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.