SwiftUI navigationLink错误-不接受链接视图上的参数



我一直在探索在swiftUI中使用地图,并从我的数据中添加了注释,但是当我在注释中添加导航链接时,我收到了一个错误-"上下文闭包类型'()->siteView'期望0个参数,但1被用于闭包正文">问题是siteView()确实期望通过一个站点。

问题是-是否有一种方法来解决这个问题,并通过siteView()屏幕创建注释的网站?

下面的代码:

struct siteMapView: View {
@EnvironmentObject var siteData: siteData
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10))
var body: some View {
NavigationView {
Map(coordinateRegion: $region, annotationItems: siteData.sites) {
MapAnnotation(coordinate: $0.locationCoordinate, anchorPoint: CGPoint(x: 0.5, y: 0.5)) {
NavigationLink {
siteView(site: $0)
} label: {
Circle()
.strokeBorder(Color.green, lineWidth: 10)
.frame(width: 44, height: 44)
}
}
}
.edgesIgnoringSafeArea(.top)
}
}

}

siteView完整代码:

struct siteView: View {
@EnvironmentObject var siteData: siteData
var site: Site
var body: some View {
ScrollView {
VStack {
ZStack{
AsyncImage(url: URL(string: "https://overseer.cyou/heritage/images/(site.imageFile).jpg")) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.clipped()
.aspectRatio(1, contentMode: .fill)
.frame(maxWidth: .infinity)
case .failure:
Text("Failure")
Image(systemName: "photo")
@unknown default:
EmptyView()
}
}
VStack {
Spacer()
VStack {
Text(site.name)
.font(.title)
.foregroundColor(Color.white)
Text(site.country)
}
.frame(maxWidth: .infinity)
.background(Color.black)
//.opacity(0.2)
}
}
Text(site.description)
}
}
}
}

}

Map(coordinateRegion: $region, annotationItems: siteData.sites) { site in
MapAnnotation(coordinate: site.locationCoordinate, anchorPoint: CGPoint(x: 0.5, y: 0.5)) {
NavigationLink {
//      '$0' ↑
//  you can't use '$0' here 
//  siteView(site: $0)

siteView(site: site)
} label: {
Circle()
.strokeBorder(Color.green, lineWidth: 10)
.frame(width: 44, height: 44)
}
}
}
.edgesIgnoringSafeArea(.top)

相关内容

  • 没有找到相关文章

最新更新