SwiftUI: Putting multiple BindableObjects into Env

2020-07-02 03:54发布

In SwiftUI it is possible to use the environmentObject method of the View object to put a single BindableObject into the environment.

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.

2条回答
戒情不戒烟
2楼-- · 2020-07-02 04:35

According to the Apple Official guide I'm guessing if each view just has one environmentObject then doing that would be fine. When there happened to be more than one, I'm not sure how the object are referenced.

查看更多
看我几分像从前
3楼-- · 2020-07-02 04:40

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)
查看更多
登录 后发表回答