如何在 UIRepresentable 的协调器类中获取值? [英] How to get at a value in a coordinator class of a UIRepresentable?

查看:46
本文介绍了如何在 UIRepresentable 的协调器类中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 charts 包 来显示海拔,当我点击图表时,我得到该点的索引,我想使用该索引在其上方的地图上放置一个标记.即locations[index].coordinate.

I am using the charts package to display the elevation, when I tap the chart I get the index of the point and I want to use that index to place a marker on the map above it. I.e locations[index].coordinate.

我有以下代码,我试图将索引值传递给我的 mapView,它也使用 UIRepresentable 来显示地图,但我很难弄清楚如何连接到发布者.

I have the following code and I am trying to pass the index value to my mapView that is also using UIRepresentable to display the map, but I'm having a hard time figuring out how to connect to the publisher.

如何将 Coordinator 类中的 @Published 连接到 MapKitView,以便将索引视为 Int?

How do I connect the @Published in the Coordinator class to the MapKitView, so that index will be seen as an Int?

import SwiftUI
import CoreLocation
import Charts

struct ElevationGraphView: UIViewRepresentable  {
    typealias UIViewType = LineChartView
    

    var locations: [CLLocation]?
    let formatter = DateFormatter()
    
    class Coordinator: NSObject, ChartViewDelegate,  ObservableObject {
        
        @Published var index: Int = 0
        
        var parent: ElevationGraphView
        init(_ parent: ElevationGraphView) {
            self.parent = parent
        }
        
        func chartValueSelected(_ uiView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
            
            if let dataSet = uiView.data?.dataSets[highlight.dataSetIndex] {
                let value = dataSet.entryIndex(entry: entry)
                print("Index: \(value)")
                
                // How to get value?
            }
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

struct RaceDetailView: View {
   var body: some View {
        VStack {
            MapKitView(locations: raceDetails.locations, markers: raceDetails.pins, region: raceDetails.region)
            ElevationGraphView(locations: raceDetails.locations)

struct MapKitView: UIViewRepresentable {
    
    let locations: [CLLocation]?
    let markers: [Pin]?
    let region: MKCoordinateRegion?

     ...

    let racePointMarker = RaceMarker(coordinate: locations[index].coordinate, title: "", tint: Color.black)
        mapView.addAnnotation(racePointMarker)
 

推荐答案

在我看来,最简单的方法是使用外部 observable(以避免将所有这些事情紧密结合在一起).

Most simple, as I see, would be to use external observable (to avoid tight all those things together).

这是可能的方法(因此两个视图彼此都不了解).

Here is possible approach (so both view don't know anything about each other).

class ChartBridge: ObservableObject {
    let shared = ChartBridge()
    @Published var selectedIndex: Int = 0
}

并且您不需要使内部协调器可观察(这是毫无意义的)

and you don't need to make internal coordinator observable (that is senseless)

class Coordinator: NSObject, ChartViewDelegate {
    
    // ... other code
    
    func chartValueSelected(_ uiView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        
        if let dataSet = uiView.data?.dataSets[highlight.dataSetIndex] {
            let value = dataSet.entryIndex(entry: entry)
            print("Index: \(value)")
            
            ChartBridge.shared.selectedIndex = value       // << here set !!
        }
    }
}

并在消费者地图视图中观察它

and in consumer map view just observe it

struct MapKitView: UIViewRepresentable {
    @ObservedObject var chart = ChartBridge.shared

    // ... other code

    let racePointMarker = RaceMarker(coordinate: 
        locations[chart.selectedIndex].coordinate,    // << here consume !!
        title: "", tint: Color.black)
        mapView.addAnnotation(racePointMarker)

这篇关于如何在 UIRepresentable 的协调器类中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆