I was able to make it work when I have two views where firstView knows that if second view was dismissed or not.
- First View presents Second View
- Upon dismissing second view, first view knows it was dismissed
here is the code
struct FirstView: View {
@State var openSecondView: Bool = false
var body: some View {
Button.init {
openSecondView = true
} label: {
Text.init("Click here")
}.sheet(isPresented: $openSecondView) {
if openSecondView {
SecondView.init(bindSecondView: $openSecondView)
}
}
}
}
struct SecondView: View {
@Binding var bindSecondView: Bool
var body: some View {
Button.init {
bindSecondView = false
} label: {
Text.init("Click here to dismiss sheet")
}
}
}
It is working as expected.
I have another usecase where I have three views. First view should know what user did in third view and I am having hard time to understand this. For the sake of simplicity I added below example.
struct FirstView: View {
@State var openSecondView: Bool = false
var body: some View {
Button.init {
openSecondView = true
} label: {
Text.init("Click here to show second view")
}.sheet(isPresented: $openSecondView) {
SecondView.init(bindSecondView: $openSecondView)
}
}
}
struct SecondView: View {
@Binding var bindSecondView: Bool
var body: some View {
Button.init {
bindSecondView = false
} label: {
Text.init("Click here to dismiss sheet")
}
ThirdView.init()
}
}
struct ThirdView: View {
var body: some View {
Button.init {
//user did something
} label: {
Text.init("By clicking this button in third view, first view should know about this so I can do some updates in first view")
}
}
}
How do I properly pass properties and observe it between these views so that first view know that a button was tapped in third view.
Any help is appreciated.