0

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.

user4150758
  • 384
  • 4
  • 17
  • So no data is to be passed back to view 1, it's only an event (button click) that is interesting? Off topic but you don't need to write `.init` everywhere in your view code – Joakim Danielson Oct 27 '21 at 14:31
  • yeah no data need to be passed because my usecase only requires updating the whole first view. Regarding .init - Xcode is kind of annonying to show completions but when you input .init it is quick :) – user4150758 Oct 27 '21 at 14:47
  • 1
    Does this answer your question https://stackoverflow.com/a/60904001/12299030? – Asperi Oct 27 '21 at 15:29
  • @Asperi the example from that link is similar as the one that I've already done(see my first example). – user4150758 Oct 27 '21 at 15:42
  • actually I was able to make it work from the link you provided with some changes...thanks @Asperi – user4150758 Oct 27 '21 at 16:15

0 Answers0