要将EnvironmentObject传递给EnvironmentObject中的ObservedObject,需要遵循以下步骤:
class EnvironmentData: ObservableObject {
@Published var value: Int = 0
}
class ObservedData: ObservableObject {
@EnvironmentObject var environmentData: EnvironmentData
}
let environmentData = EnvironmentData()
let contentView = ContentView()
.environmentObject(environmentData)
window.rootViewController = UIHostingController(rootView: contentView)
struct ContentView: View {
@ObservedObject var observedData = ObservedData()
var body: some View {
Text("Value: \(observedData.environmentData.value)")
}
}
这样,EnvironmentObject将会被传递给EnvironmentObject中的ObservedObject。每当EnvironmentData中的value发生变化时,视图将自动更新以显示最新的值。
关于腾讯云相关产品和介绍链接,由于不允许提及特定品牌商,建议您在腾讯云的官方网站或相关文档中搜索和了解与云计算相关的产品和服务。
领取专属 10元无门槛券
手把手带您无忧上云