I’ve an intensive UIKit view controller with many capabilities that I’m pulling from the Mapbox Navigation SDK instance right here. I’d wish to develop in SwiftUI so I am studying the right way to combine SwiftUI with UIKit. Undecided what my best choice is on this situation.
The view controller has properties and capabilities that I’d like to reveal to my SwiftUI view. Akin to a waypoints array property, a boolean property I create for when route preview is showcased, a startNavigation operate, requestFollowCamera operate e.t.c
On this situation, I’ve created a UIViewControllerRepresentable above my Viewcontroller such that I can reuse the View in my SwiftUI ContentView and set off the startNavigation operate by means of a certain state like this:
ViewController.Swift
struct NavViewController: UIViewControllerRepresentable {
@Binding var startNavigation: Bool
func makeUIViewController(context: Context) -> ViewController {
return ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
if startNavigation{
uiViewController.startBasicNavigation()
}
}
}
ContentView.Swift
struct ContentView: View {
@State var startNavigation: Bool = false
var physique: some View {
ZStack{
NavViewController(startNavigation: $startNavigation)
.ignoresSafeArea()
Button("Begin Navigation"){
startNavigation.toggle()
}
}
}
}
However I’d additionally wish to entry a present route preview bool from my viewcontroller in SwiftUI. I’m undecided how I’d have the ability to expose the properties in my opinion controller to swiftUI.
I’m considering the answer could be to make use of cordinators, publish properties or observable object. This is what I’ve tried doing with no luck: Modified Content material View to make use of an observable object and declared var routeisAvailable:RouteAvailability?
of sort observable object in my opinion controller.
struct ContentView: View {
@State var startNavigation: Bool = false
@ObservedObject var routeAvailability: RouteAvailability = RouteAvailability()
var physique: some View {
ZStack{
NavViewController(startNavigation: $startNavigation)
.ignoresSafeArea()
if routeAvailability.isAvailable{
Button("Begin Navigation"){
startNavigation.toggle()
}
}
}
}
}