Итак, в MapView мне нужен пользовательский инициализатор, как определено ниже.
import SwiftUI
import Mapbox
struct MapView: UIViewRepresentable {
var configure: (MGLMapView) -> () = { _ in }
@Binding var showMoreDetails: Bool
var VModel: ViewModel
var token: String
init(showMoreDetails: Binding<Bool>, VModel: ViewModel, token: String) {
self.VModel = VModel
self.token = token
_showMoreDetails = showMoreDetails
}
//This function creates the actual view of the map
func makeUIView(context: Context) -> MGLMapView {
let map = MGLMapView()
DispatchQueue.main.async {
map.delegate = context.coordinator
self.configure(map)
}
return map
}
//This function is called whenever there is a change in the map's state
//Go over the functionality of this with TJ.
func updateUIView(_ uiView: MGLMapView, context: Context) {
}
}
В ContentView я хотел бы инициализировать представление следующим образом:
import SwiftUI
import Mapbox
struct ContentView: View {
@ObservedObject var VModel: ViewModel = ViewModel()
@State private var showMoreDetails: Bool = true
@State private var token: String = "test"
@State private var centerToUser: () -> () = { }
var body: some View {
ZStack {
MapView(showMoreDetails: $showMoreDetails, VModel: VModel, token: token) { map in
self.centerToUser = {
print("run code!")
}
}
Button("test", action: centerToUser)
}
}
}
Я считаю, что проблема в настраиваемом инициализаторе, который я определил в структуре MapView. Когда я удаляю это закрытие рядом с ним в ContentView, оно работает. Как мне отредактировать настраиваемый инициализатор, чтобы он также принимал закрытие при инициализации MapView?