I have a BaseView something like this and I want to add alert mechanism to all my subviews
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"))
}
}
}
And here is my SubView.
struct SubView: View {
BaseView {
Text("")
}.onReceive(vm.publisher) { (output) in
// here I want to trigger BaseView isAlertPresented property
// BaseView.isAlertPresented = true
}
}
Can I do something like this? If yes how can I?
If view contains BaseView it is definitely not a subview relating to it. And you should not access/manipulate internal state of other view due to single-source-of-truth violation.
Instead you have to use Binding in this scenario, like below (tested with Xcode 11.7)