我正在使用 Swiftui 列表来显示任务。每个任务都有一个位置。当您单击列表行时,我正在使用 NavigationLink 导航到详细信息页面。
我正在使用 UIViewControllerRepresentable 创建一个地图视图,其中所有任务位置都在地图上进行了注释。我必须使用 UIKit 以便您可以与注释进行交互。我设置了一个在用户单击地图注释附件时触发的委托。
目的:当调用委托函数时,我想传回用于导航到详细信息页面的任务 ID,例如单击列表行时:
我从列表中导航的方式:
ForEach(tasksToDisplayInList, id: \.id) { task in
NavigationLink(
destination: TaskDetailsView(taskId: task.id, title: task.title))
{
TaskListRow(task: task)
.padding(.trailing, 5)
.foregroundColor(.textDefault)
}
}
传回按下的任务 id 的任务列表映射 UIViewControllerRepresentable:
struct TaskListMap: UIViewControllerRepresentable {
typealias UIViewControllerType = TaskListMapController
@Binding var annotations: [TaskAnnotation]
class Coordinator: TaskListMapDelegate {
func didPressMoreDetail(_ mapController: TaskListMapController, for annotation: TaskAnnotation) {
// TaskAnnotation contains the taskId
// Navigate here somehow?
}
var parent: TaskListMap
init(_ parent: TaskListMap) {
self.parent = parent
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> TaskListMapController {
let mapController = TaskListMapController(annotations: annotations)
mapController.delegate = context.coordinator
return mapController
}
func updateUIViewController(_ uiViewController: TaskListMapController, context: Context) {
if annotations.count != uiViewController.mapView.annotations.count {
uiViewController.mapView.removeAnnotations(uiViewController.mapView.annotations)
uiViewController.mapView.addAnnotations(annotations)
}
}
}