【发布时间】:2021-06-16 10:36:44
【问题描述】:
我有一个 SwiftUI 应用程序,我在其中显示了一个带有不同注释的地图(使用由 UIViewRepresentable 制作的 Mapkit)。在每个注释上,我都实现了一个标注按钮,我想在按下按钮时完全改变主视图。
我的想法是使用主视图和地图视图的状态/绑定参数。这个想法有效,但我在使用地图视图的方式实现状态/绑定时遇到了一些困难。困难在于我使用地图作为第二个视图的参数,该视图包含与地图交互的菜单。我试图改变我使用地图视图的方式,但我没有找到任何解决方案...
我的想法是在 body 之前创建地图:@State var map = MapView(displaySP: $displayStorePage),但我做不到(错误:不能在属性初始化程序中使用实例成员 '$displayStorePage';属性初始化程序在 'self' 可用之前运行)
非常欢迎任何帮助:)
edit: 附加代码显示,我尝试做最轻的版本。
所以在代码中,目标是从为注释点击func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)激活的Mapview函数切换Mainview中的displayStorePage
主视图:
import SwiftUI
import MapKit
struct MainView: View {
@State var researchStore: String = ""
@State var displayStorePage: Bool = false
@State var map = MapView()
var body: some View {
ZStack{
if displayStorePage == false {
VStack{
map.frame(height: 400)
}
TopMapMenu(map: $map)
}
if displayStorePage == true {
Text("This is a win !")
}
}
}
}
MapKit 视图:
import Combine
var storeLocation = [MKAnnotation]()
var newAnnotation = MKPointAnnotation()
let mapView = MKMapView()
var displayRedMarker: Bool = true
var displayStore: Bool = false
struct MapView: UIViewRepresentable {
class Coordinator: NSObject, MKMapViewDelegate{
var parent : MapView
init(_ parent: MapView){
self.parent = parent
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
let annotationLabel = UILabel(frame: CGRect(x: -25, y: 12, width: 105, height: 30))
if displayRedMarker {
annotationLabel.text = annotation.title!!
} else {
annotationLabel.text = "Annotation to be hidden"
}
//Other markers done there ....
annotationView?.canShowCallout = true
let btn = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = btn
return annotationView
}
// Gestion click Annotation button
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
// Here : Changing the Main View parameter : displayStorePage to true
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> some UIView {
let region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 47.510053, longitude: 6.798374),
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.delegate = context.coordinator
mapView.showsUserLocation = true
mapView.setRegion(region, animated: false)
mapView.addAnnotation(AddAnnotations())
return mapView
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
func AddAnnotations() -> MKAnnotation {
newAnnotation = MKPointAnnotation()
newAnnotation.title = "Title1"
newAnnotation.coordinate = CLLocationCoordinate2D(latitude: 47.510053, longitude: 6.798374)
return newAnnotation
}
}
TopMenuMap:
struct TopMapMenu: View {
@Binding var map : MapView
@State var searching: Bool = false
@State var redStore: Bool = true
@State var storeFound : Bool = false
var body: some View {
HStack{
VStack(alignment: .leading, spacing: 10){
Button(action: {
displayRedMarker.toggle()
// map.DeleteMarkers()
// map.AddMarkers()
redStore = displayRedMarker
}) {
HStack {
if redStore { Image(systemName: "heart.fill") }
else { Image(systemName: "heart") }
Text("Restaurant")
}
Spacer()
.background(Color(.white))
}
// Other Buttons(Pins) are display here
}// End VStack Pin stores
}
}
}
【问题讨论】:
-
欢迎来到 SO - 请使用 tour 并阅读 How to Ask 以改进、编辑和格式化您的问题。如果没有Minimal Reproducible Example,就无法帮助您进行故障排除。