Ios @EnvironmentObject未反映在对象中

Ios @EnvironmentObject未反映在对象中,ios,swift,swiftui,Ios,Swift,Swiftui,我在源代码中使用了@EnvironmentObject,但它不能正常工作 这只是我的代码: import Foundation import MapKit class Configure: ObservableObject { @Published var mapType = MKMapType.satellite } 我在userData中有配置对象: class UserData: ObservableObject { @Published var configure:

我在源代码中使用了@EnvironmentObject,但它不能正常工作

这只是我的代码:

import Foundation
import MapKit

class Configure: ObservableObject
{
    @Published var mapType = MKMapType.satellite
}
我在userData中有配置对象:

class UserData: ObservableObject
{
    @Published var configure: Configure
}
并使用MapHome中的配置对象:

struct MapHome: View
{
    @EnvironmentObject var userData: UserData

    NavigationView
    {
        ZStack
        {
            MapView(mapViewState: mapViewHomeState)
                    .edgesIgnoringSafeArea(.all)            
        }
        Button(action: {
                    switch self.userData.configure.mapType
                    {
                        case .hybrid:
                            self.userData.configure.mapType = .standard
                        case .standard:
                            self.userData.configure.mapType = .satellite
                        case .satellite:
                            self.userData.configure.mapType = .hybrid
                        default:
                            self.userData.configure.mapType = .standard
                    }
                }
        )
        {
            if self.configure.mapType == .hybrid
            {
                Image("HybridIcon")
            }
            else if self.configure.mapType == .standard
            {
                Image("StandardIcon")
            }
            else if self.configure.mapType == .satellite
            {
                Image("SatelliteIcon")
            }
        }

    }
}
MapView就是这样:

import SwiftUI
import MapKit

struct MapView: UIViewRepresentable
{ 
    @ObservedObject var configure: Configure

    func makeUIView(context: Context) -> MKMapView
    {
        return MKMapView(frame: .zero)
    }

    func updateUIView(_ view: MKMapView, context: Context)
    {
        //Set the map type
        view.mapType = configure.mapType        
    }    
}
单击该按钮时,源代码已转到MapView(mapViewState:mapViewHomeState).edgesIgnoringSafeArea(.all),但未调用MapView.UpdateUI!因此,地图视图不会使用MKMapType刷新

我的代码怎么了?我该怎么办?
非常感谢

在哪里指定环境对象

通常,您会这样做:

MapHome().environmentObject(UserData())

您需要在SceneDelegate.swift文件中指定环境对象,并且可以从整个层次结构中访问该对象。 应该是这样的:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    // Use a UIHostingController as window root view controller
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: MapHome().environmentObject(UserData())
        self.window = window
        window.makeKeyAndVisible()
    }
}

正如@Sorillica在评论中提到的,此时

最简单的方法可能是将
Configure
对象直接添加到环境中(它是一个类,因此它将与
UserData
实例引用的实例相同)

在您的
SceneDelegate
-

let userData = UserData()
let contentView = MapHome().environmentObject(userData).environmentObject(userData.configure)
然后您可以在
地图主页中使用它

struct MapHome: View
{
    @EnvironmentObject var userData: UserData
    @EnvironmentObject var configure: Configure

    NavigationView
    {
        ZStack
        {
            MapView(mapViewState: $configure.mapType)
                    .edgesIgnoringSafeArea(.all)            
        }
        Button(action: {
                    switch self.userData.configure.mapType
                    {
                        case .hybrid:
                            self.userData.configure.mapType = .standard
                        case .standard:
                            self.userData.configure.mapType = .satellite
                        case .satellite:
                            self.userData.configure.mapType = .hybrid
                        default:
                            self.userData.configure.mapType = .standard
                    }
                }
        )
        {
            if self.userData.configure.mapType == .hybrid
            {
                Image("HybridIcon")
            }
            else if self.userData.configure.mapType == .standard
            {
                Image("StandardIcon")
            }
            else if self.userData.configure.mapType == .satellite
            {
                Image("SatelliteIcon")
            }
        }

    }
}
struct MapHome: View
{
    @EnvironmentObject var userData: UserData
    @State var mapType: MKMapType

    NavigationView
    {
        ZStack
        {
            MapView(mapViewState: $mapType)
                    .edgesIgnoringSafeArea(.all)            
        }
        Button(action: {
                    switch self.userData.configure.mapType
                    {
                        case .hybrid:
                            self.userData.configure.mapType = .standard
                        case .standard:
                            self.userData.configure.mapType = .satellite
                        case .satellite:
                            self.userData.configure.mapType = .hybrid
                        default:
                            self.userData.configure.mapType = .standard
                    }
                    self.mapType = self.userdata.configure.mapType
                }
        )
        {
            if self.userdata.configure.mapType == .hybrid
            {
                Image("HybridIcon")
            }
            else if self.userdata.configure.mapType == .standard
            {
                Image("StandardIcon")
            }
            else if self.userdata.configure.mapType == .satellite
            {
                Image("SatelliteIcon")
            }
        }

    }
}
您的
地图视图
将需要
地图类型
作为
@绑定
-

struct MapView: UIViewRepresentable
{
    @Binding var mapType: MKMapType

    func makeUIView(context: Context) -> MKMapView
    {
        return MKMapView(frame: .zero)
    }

    func updateUIView(_ view: MKMapView, context: Context)
    {
        //Set the map type
        view.mapType = mapType
    }
}
另一种方法是将映射类型镜像到
MapHome中的
@State
属性

struct MapHome: View
{
    @EnvironmentObject var userData: UserData
    @EnvironmentObject var configure: Configure

    NavigationView
    {
        ZStack
        {
            MapView(mapViewState: $configure.mapType)
                    .edgesIgnoringSafeArea(.all)            
        }
        Button(action: {
                    switch self.userData.configure.mapType
                    {
                        case .hybrid:
                            self.userData.configure.mapType = .standard
                        case .standard:
                            self.userData.configure.mapType = .satellite
                        case .satellite:
                            self.userData.configure.mapType = .hybrid
                        default:
                            self.userData.configure.mapType = .standard
                    }
                }
        )
        {
            if self.userData.configure.mapType == .hybrid
            {
                Image("HybridIcon")
            }
            else if self.userData.configure.mapType == .standard
            {
                Image("StandardIcon")
            }
            else if self.userData.configure.mapType == .satellite
            {
                Image("SatelliteIcon")
            }
        }

    }
}
struct MapHome: View
{
    @EnvironmentObject var userData: UserData
    @State var mapType: MKMapType

    NavigationView
    {
        ZStack
        {
            MapView(mapViewState: $mapType)
                    .edgesIgnoringSafeArea(.all)            
        }
        Button(action: {
                    switch self.userData.configure.mapType
                    {
                        case .hybrid:
                            self.userData.configure.mapType = .standard
                        case .standard:
                            self.userData.configure.mapType = .satellite
                        case .satellite:
                            self.userData.configure.mapType = .hybrid
                        default:
                            self.userData.configure.mapType = .standard
                    }
                    self.mapType = self.userdata.configure.mapType
                }
        )
        {
            if self.userdata.configure.mapType == .hybrid
            {
                Image("HybridIcon")
            }
            else if self.userdata.configure.mapType == .standard
            {
                Image("StandardIcon")
            }
            else if self.userdata.configure.mapType == .satellite
            {
                Image("SatelliteIcon")
            }
        }

    }
}
您需要在
SceneDelegate

 let userData = UserData()
 let contentView = MapHome(mapType: userData.configure.mapType).environmentObject(userData)

您对
MapView
的声明与调用它的方式不匹配。该属性在声明中命名为
configure
,但在调用中命名为
mapViewState
。请检查此项