How to connect SwiftUI view to Tab View Controller

Hello everyone reading out this post for me. :blush: I am new to swiftui. I want to connect a tab bar view controller to swift ui view. This is the swiftui view I want to incorporate in a tab bar view controller GitHub - khalid-asad/card-reader-ios: A credit card reader and parser for iOS Using Native Vision/VisionKit

I am facing problem in connecting a swiftui view with a tab bar view controller. This is what I have done so far.
Step1: I have created a hosting view controller. Step 2: established a root view controller relationship segue between the hosting view controller and navigation/tab bar controller. Step 3: Now i have a swiftUI view named as CardFormView that i want to link. Step 4: create a UIHostingController class and set the HostingViewController to that class in the class inspector.
This is how my hosting view controller file looks like

import UIKit
import SwiftUI

class HostingViewController: UIHostingController<CardFormView> {

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder, rootView: CardFormView( completion: (CardDetails) -> Void))
    }
}

But I am getting an error Cannot convert value of type ‘((CardDetails) → Void).Type’ to expected argument type ‘(CardDetails) → Void’
I am not able to understand this swiftui code much.

It would be helpful if I can get some help in understanding the library code. I have seen the functionality of this app and am familiar with the file types. But I am not sure how to fix this issue in order to connect a tab bar view controller to swiftui view successfully.
It would be helpful if i can get some help. :pray:

Welcome back shilpee,

it seems that Xcode is complaining here because it expects a closure for the completion handler of CardFormView.

In this case the CardFormView does not take the (CardDetails) -> Void) closure type, but the actual closure value. So you would have something like this instead:

CardFormView(completion: { cardDetails in

})

and since the last argument is a completion handler you can apply some syntactic sugar (trailing closure syntax) and write it as:

CardFormView { cardDetails in

}

Hope this helps with your problem.

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.