显示 Alert
的大多数示例都引用某种 @State
用作控制呈现/隐藏状态的绑定(bind)警报 View 。
作为示例showingAlert
(source):
struct ContentView : View {
@State var showingAlert = false
var body: some View {
Button(action: {
self.showingAlert = true
}) {
Text("Show Alert")
}
.alert(isPresented: $showingAlert) {
Alert(
title: Text("Important message"),
message: Text("Wear sunscreen"),
dismissButton: .default(Text("Got it!"))
)
}
}
}
当从 UI 层触发警报时,这是一个很好的解决方案 - 如示例所示:
Button(action: {
self.showingAlert = true
}
但是如果我们想使用特定消息从 Controller / View 模型层触发它怎么办?举个例子,我们进行一个网络调用 - URLSession
的Publisher
可以发送 Data
或 Error
,我们希望将其作为 Alert
中的消息推送给用户。
@State
被设计为从 View 的 body
进行管理,因此在这种情况下我们似乎应该使用 @ObjectBinding
。看来我们还需要一些message
,所以我们可以在body
中引用它:
Alert(
title: Text("Important message"),
message: Text(objectBinding.message)
)
这里的 showingAlert
有点多余,因为我们可以将 message
定义为 String?
并为 presentation< 创建绑定(bind)
:
Binding<Bool>(
getValue: { objectBinding.message != nil },
setValue: { if !$0 { objectBinding.message = nil } }
)
这是一种可行的方法并且有效,但有两件事让我有点焦虑:
消息
由两个抽象管理的事实- 警报的呈现/隐藏状态的信息和管理泄漏到 Controller / View 模型/对象绑定(bind)中。最好将呈现/隐藏状态保留在 View 中。
- 消息一直保留在 Controller / View 模型/对象绑定(bind)中,直到它被 View (绑定(bind))“消耗”为止。
可以做得更好吗?
最佳答案
如果您特别想使用合并及其发布者机制,您可以使用onReceive()
。每个通用 SwiftUI View 都有 onReceive()
将其作为接受发布者的通用函数,并且在实例化时将订阅发布者。它的行为与合并订阅者的单闭包版本非常相似 sink
如果您熟悉的话。
发布者的具体情况期望发布者失败类型为“从不”才能正常工作,因此,如果您在某个管道中处理错误,则需要将它们转换为其他类型的对象(可能是 Enum
以及关联的 String
值)并将它们外部化,以便可以使用 SwiftUI 显示它们。
最终如何将发布者暴露给 SwiftUI View 可能也很尴尬 - 我一直通过将发布者添加到具有其他 @Published
属性的引用对象上来实现这一点。
您可以在 Using Combine 中看到其工作原理的粗略示例(尽管不是您的特定用例)在https://heckj.github.io/swiftui-notes/#pattern-observableobject
关于ios - 在 View 外部触发时如何绑定(bind) SwiftUI.Alert 的呈现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56762294/