English 中文(简体)
环境 目标正在更新(或未更新)
原标题:Mystified by how EnvironmentObject is being updated (or not)

我有一个环境目标变量,正在不同地点使用。

我愿用一个 but子更新。

.alert 援引了环境目标以变性者提供的名称更新的重罪。

Puzzle is that within the func. The EnvironmentObject on changing. 在第一次使用时,它显示了初始价值。 但是,当地图(......美元)使用时,似乎已经更新。 后来对纽顿的利用表明,金库中的数值总是从最后更新开始。

我想在其他地方使用环境目标,但总是落后。

这里是载有一些意见的法典。 如果有另一种方式,请建议。

import SwiftUI
import MapKit

struct LocationFinderViewCopy: View {
    @EnvironmentObject  var mapAPI: MapAPI  // I am using this elsehwere
    @State private var showingAlert = false
    @State private var locationName = ""
    
    
    
    var body: some View {
        VStack {
            Map(position: $mapAPI.locations[0].cameraPosition) {  //$mapAPI shows the most recent update, despite what I see in the findlocation function
                let temp = mapAPI.locations[0].coordinate
                let tempCam = mapAPI.locations[0].cameraPosition.region
                Annotation(mapAPI.locations[0].name, coordinate:  mapAPI.locations[0].coordinate, anchor: .center) {
                    MapMarkerView()
                }
            }
   
            Button("Alert find Button") {
                showingAlert.toggle()
            }
            .alert("Location for find", isPresented: $showingAlert) {
                TextField("location", text: $locationName)
                Button("Cancel", action: doNothing)
                Button("OK", action: findLocation)
            } message: {
                Label("name", systemImage: "circle.circle")
            }
        }
        
    }
    func doNothing() {
    }
    func findLocation() {
        let tempCoord1 = mapAPI.locations[0].coordinate  // mapAPI original intiation
        let tempCam1 = mapAPI.locations[0].cameraPosition  //checking value
        let name = locationName
        mapAPI.getLocation(address: locationName, delta: 10)
        let tempCoord2 = mapAPI.locations[0].coordinate  // expected mapAPI to reflect it is still showing init values.  Each subsequent entry shows the previous request.  same value as tempCoord1
        let tempCam2 = mapAPI.locations[0].cameraPosition
    }
}

#Preview {
    LocationFinderViewCopy()
}




努力更新环境目标和更新内容,见于各条。

问题回答




相关问题
SwiftUI: How do I position a button x points above a sheet?

I am trying to recreate a Maps-style UI with a persistent bottom sheet and controls fixed above. Apple Maps UI with controls above ☝️ Example here, the "Look Around" binoculars button and ...

How to access own window within SwiftUI view?

The goal is to have easy access to hosting window at any level of SwiftUI view hierarchy. The purpose might be different - close the window, resign first responder, replace root view or ...

热门标签