0

I'm a beginner of SwiftUI. (Sorry in advance if my English is hard to understand.)
I want to enable multiple screen transitions using Modal.
【Go back to the initial view when the view reached to the last View and pressed the button】 That's what I want to realize.
I thought my code would work perfectly but when I pressed the button of last view it stopped at the second view, not the initial view.
Can't figure out what's wrong and where to fix, any solutions?

Here's my code.

`

import SwiftUI

struct ContentView: View {
    @State var isShowSecondView = false
    var body: some View {
        Button("To SecondView") {
            isShowSecondView = true
        }
        .font(.largeTitle)
        .fullScreenCover(isPresented: $isShowSecondView) {
            SecondView(isShowSecondView: $isShowSecondView)
        }
    }
}

struct SecondView: View {
    @Binding var isShowSecondView: Bool
    @State var isShowThirdView = false
    var body: some View {
        Button("To ThirdView") {
            isShowThirdView = true
        }
        .font(.largeTitle)
        .fullScreenCover(isPresented: $isShowThirdView) {
            ThirdView(isShowNextView: $isShowSecondView,
                      isShowThirdView: $isShowThirdView)
        }
    }
}

struct ThirdView: View {
    @Binding var isShowNextView: Bool
    @Binding var isShowThirdView: Bool
    @State var isShowForthView = false
    var body: some View {
        Button("To ForthView") {
            isShowForthView = true
        }
        .font(.largeTitle)
        .fullScreenCover(isPresented: $isShowForthView) {
            ForthView(isShowNextView: $isShowNextView,
                      isShowThirdView: $isShowThirdView, isShowForthView: $isShowForthView)
        }
    }
}

struct ForthView: View {
    @Binding var isShowNextView: Bool
    @Binding var isShowThirdView: Bool
    @Binding var isShowForthView: Bool
    var body: some View {
        Button("Back to FirstView") {
            isShowNextView = false
            isShowThirdView = false
            isShowForthView = false
        }
        .font(.largeTitle)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

`

Yuta
  • 1
  • 1
  • You can´t use the `Binding` value inside `.fullScreenCover` with the `.fullScreenCover(isPresented:` constructor. It won´t update and will show the observed behaviour. You could try to use the `.fullScreenCover(item: ` constructor like in this [answer](https://stackoverflow.com/a/71943044/6950415). – burnsi Dec 02 '22 at 13:34
  • Problem solved by using 【.fullScreenCover(item:】as you told, thanks! – Yuta Dec 04 '22 at 22:27

0 Answers0