我有一个类似这样的 BaseView,我想为我的所有子视图添加警报机制
struct BaseView<Content: View>: View {
@State var isAlertPresented = false
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body : some View {
content.alert(isPresented: $isAlertPresented) {
Alert(title: Text("title"))
}
}
}
这是我的子视图。
struct SubView: View {
BaseView {
Text("")
}.onReceive(vm.publisher) { (output) in
// here I want to trigger BaseView isAlertPresented property
// BaseView.isAlertPresented = true
}
}
我可以做这样的事情吗?如果是,我该怎么办?