SwiftUI:代理协议的替代品是什么?

huangapple go评论62阅读模式
英文:

SwiftUI: what substitutes the Delegate protocol?

问题

让我们考虑一种最基本的流程,其中您拥有一个带有数据的视图,该数据是使用其他次要视图填充的,即设置。

在“设置”视图中,我们显示了2个项目:

SETTINGS
A - 选择颜色
B - 选择国家

当用户点击其中一个项目时,应用程序将呈现一个新视图,可用于选择颜色或另一个视图,可用于选择国家。

当用户完成一个选择视图后,“选择视图”关闭,并将选择传递回“设置”视图,该视图现在显示新信息:

SETTINGS
A - 绿色
B - 西班牙

使用“代理”和“协议”,我们可以轻松实现此代码,只需将“设置”视图设置为“颜色”和“国家”视图的代理,并实现一个特定的方法,直接在“设置”视图上接收用户选择。

如何使用SwiftUI实现相同的流程呢?
我正在阅读关于@EnvironmentObject的内容,这是否是@EnvironmentObject适用的情况?您将如何使用@EnvironmentObject实现上述描述的情况?

注意:我看到了其他类似的问题,但它们对于单个情况过于具体。我的问题较为广泛,通常适用于许多不同情况。

英文:

Let's consider one of the most basic flow available, where you have a view with data that is filled using other secondary views. i.e. Settings.

In the Settings view we have 2 items displayed:

SETTINGS
A - Pick a Color 
B - Pick a Country 

When user taps one of the items, the App will present a new view that can be used to select a color or another view that can be used to select a country.

When the user is done with one of the selection views, the Selection View is closed and the selection is passed back to the Settings view that now displays the new info:

SETTINGS
A - Green 
B - Spain

With delegates and protocols we can easily implement this code just putting the Settings view as delegate for the Colors and the Countries view and implementing a specific method to receive user selection directly on the Settings view.

How can we achieve the same flow with SwiftUI?
I'm reading about @EnvironmentObject, is this a case where @EnvironmentObject would work? how would you implement the case described above with @EnvironmentObject?

Note: I've seen other similar questions but they were too specific for single cases. My question is broad and should generically apply to many differente cases.

答案1

得分: 1

我发现一个可能的解决方案(而不是使用 @EnvironmentObject)是使用 @Binding

对于 设置视图,我可以将 selectedColorselectedCountry 声明为 @State

@State private var selectedColor: String = ""
@State private var selectedCountry: String = ""

让我们实现 颜色选择视图。我可以这样设置一个 bind 到一个 color 变量:

@Binding var myColor: String = ""

现在,如果 myColor 的值发生变化,由于该变量是一个绑定,对于引用该变量的视图(在这种情况下是 设置视图),它也会发生变化。

设置视图 中,只需要在呈现 color view 时设置绑定:

NavigationLink(destination: ColorView(myColor: self.$selectedColor)) {

我认为这是替代 Delegate 逻辑的一种好方法。

英文:

I've found that a possible solution (instead of using @EnvironmentObject) would be to use @Binding.

For the Settings View I can have selectedColor and selectedCountry as @State

@State private var selectedColor:String = ""
@State private var selectedCountry:String = ""

Let's implement the Color Selection View. I can set a bind to a color variable this way:

@Binding var myColor:String = ""

Now if the value of myColor changes, since the variable is a Bind it will also change for the view that has the reference to this variable (in this case, the Settings view)

From the Settings view it would be enough to set the binding while presenting the color view.

NavigationLink(destination: ColorView(myColor: self.$selectedColor)) { 

In my opinion this would be a good way to substitute the Delegate logic.

huangapple
  • 本文由 发表于 2020年1月3日 23:13:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定