SwiftUI:将多个BindableObjects放入环境 [英] SwiftUI: Putting multiple BindableObjects into Environment
问题描述
在 SwiftUI
中,可以使用View对象的 environmentObject
方法放置一个 BindableObject
进入环境。
In SwiftUI
it is possible to use the environmentObject
method of the View object to put a single BindableObject
into the environment.
如果我想放置多个 BindableObjects
同时进入环境?我在SwiftUI文档中没有找到任何解决方案。我不想在构造函数中传递对象。
What if I want to put multiple BindableObjects
at the same time into the environment? I don't see any solution for this in the SwiftUI documentation. I don't want to have to pass the objects in the constructor.
推荐答案
对 environmentObject的调用()
返回一个(修改的)视图,因此您可以链接将多个对象放入环境的调用。示例:
The call to environmentObject()
returns a (modified) view, therefore you can chain the calls to put multiple objects into the environment. Example:
let rootView = ContentView()
.environmentObject(firstBindable)
.environmentObject(secondBindable)
这篇关于SwiftUI:将多个BindableObjects放入环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!