如何设置相同类型的多个EnvironmentObjects [英] How to set multiple EnvironmentObjects which are same type
问题描述
我发现了这个问题 SwiftUI:将多个BindableObjects放入环境a>
I found this question SwiftUI: Putting multiple BindableObjects into Envionment
答案说,environmentObject(ObservableObject)返回修改后的视图,因此我可以为多个环境对象建立调用链.
the answer said environmentObject(ObservableObject) returns modified view, therefore I can make call chain for multiple environmentObject.
喜欢
let rootView = ContentView()
.environmentObject(firstBindable)
.environmentObject(secondBindable)
,我想知道firstBindable和secondBindable是同一类型会导致什么结果. .environmentObject()
如何知道什么是exect值,这是程序员打算在 firstBindable
和 secondBindable
之间设置的值.
and I wonder what is result if firstBindable and secondBindable are same type. how .environmentObject()
knows what is exect value which is a programmer intended to set between firstBindable
and secondBindable
.
所以我对此进行了测试
- 我制作了一个ObservableObject类
final class TempStr: ObservableObject {
@Published var tmpStr = "temp"
init(initStr: String) {
tmpStr = initStr
}
}
- 来自sceneDelegate的environmentObject的调用链
window.rootViewController
= UIHostingController(rootView:
TestView()
.environmentObject(TempStr(initStr: "1st"))
.environmentObject(TempStr(initStr: "2nd"))
- 以及View中使用的值
struct TestView: View {
@EnvironmentObject var tmp1: TempStr
@EnvironmentObject var tmp2: TempStr
var body: some View {
Text(tmp1.tmpStr + " " + tmp2.tmpStr)
}
}
- 结果为第一名"
如果我的代码像这样调用一个.environmentObject()
And if my code calls one .environmentObject() like
TestView().environmentObject(TempStr(initStr: "1st"))
来自TestView的
tmp1和tmp2都具有相同的值 TempStr(initStr:"1st")
.看起来 .environmentObject()
调用设置了所有相同类型的值.
both tmp1 and tmp2 from TestView have same value TempStr(initStr: "1st")
. it looks like .environmentObject()
call sets all values of same type.
实际上,我知道这是行不通的,但我只是尝试使用此问题进行尝试.
Actually, I knew that it couldn't work but I just tried it for using this question.
我想知道实现我的目标的正确方法是什么.
I wonder what is correct way of achieving my goal.
谢谢
推荐答案
这有效:
场景代理:
class MyModel: ObservableObject {
@Published var tmpStr1:TempStr!
@Published var tmpStr2:TempStr!
init(initStr1: String, initStr2:String) {
tmpStr1 = TempStr(initStr: initStr1)
tmpStr2 = TempStr(initStr: initStr2)
}
}
class TempStr: ObservableObject {
@Published var tmpStr:String!
init(initStr: String) {
tmpStr = initStr
}
}
ContentView:
struct ContentView: View {
@EnvironmentObject var myModel: MyModel
var body: some View {
Text(myModel.tmpStr1.tmpStr + " " + myModel.tmpStr2.tmpStr)
}
}
当然,这可能不是您想要的,但这引出了一个问题-为什么想要两个 class
实例,而又不想在模型中?您可以轻松地拥有两个 separate 类作为模型的模型(以及 ObservableObjects
),并且如果您希望每个 SceneDelegate
有两个单独的模型实例?在那里.
Granted, this may not be what you are looking for, but that begs the question - why do want want two instances of a class
without wanting each in your model? You can easily have two separate classes as pat of your model (and as ObservableObjects
) and if you want two separate instances of your model per SceneDelegate
? There it is.
但是,如果您想要模型/类/任何对象的两个实例,则感觉像是重构的候选对象.
But if you want two instances of your model/class/whatever, it feels like a candidate for refactoring.
这篇关于如何设置相同类型的多个EnvironmentObjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!