-1

I have View with button that display sheet and after first sheet dismiss, I need to display a second sheet, when I use ObservableObject in Binding the View crash after showing the second sheet:

Root:

class Benef: ObservableObject {
    @Published var val: String = ""
}

@main
struct TestApp: App {
    @StateObject var beenf = Benef()
    var body: some Scene {
        WindowGroup {
            ContentView(testt: $beenf.val)
            
        }
    }
}

ContentView:

enum Test: Identifiable {
    case one
    case two
    
    var id: String {
        switch self {
        case .one : return "One"
        case .two : return "Two"
        }
    }
}

struct ContentView: View {
    @Environment(\.dismiss) private var dismiss
    @State private var showingFirst: Test?
    @State private var showingSecond: Test?
    @Binding var testt: String

    var body: some View {
        VStack {
            Button("Show First Sheet") {
                showingFirst = .one
            }
        }
        .sheet(item: $showingFirst) { tt in
            FirstSheetiew {
                showingSecond = .two
            }
        }
        .sheet(item: $showingSecond) { tt in
            VStack {
                Text("Second Sheet")
                Button("Dismiss") {
                    dismiss()
                }
            }
        }
    }
}

struct FirstSheetiew: View {
    @Environment(\.dismiss) private var dismiss

    let action: () -> Void
    
    var body: some View {
        VStack {
            Text("First Sheet")
            Button("Dismiss") {
                action()
                dismiss()
            }
        }
    }
    
}

Crash error:

"Application tried to present modally a view controller <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x12a01b600> that is already being presented by <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x12a814e00>."

OuSS
  • 19
  • 3
  • try moving the `.sheet(item: $showingSecond)` inside `.sheet(item: $showingFirst)`, like the example in: https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-multiple-sheets – workingdog support Ukraine Apr 04 '23 at 02:34
  • Its not what I want, I need to dismiss the first sheet then display the second one – OuSS Apr 04 '23 at 11:48

1 Answers1

1

This is because the value of showingSecond was changed before FirstSheetView was closed.

Therefore, you need to change the value of showingSecond inside onDisappear{ } of FirstSheetView.

Here is how to fix it:

struct FirstSheetiew: View {
    @Environment(\.dismiss) private var dismiss

    let action: () -> Void
    
    var body: some View {
        VStack {
            Text("First Sheet")
            Button("Dismiss") {
                //action()  <--  remove
                dismiss()
            }
        }
        .onDisappear {  //<--  add
            action()
        }
    }
}
Cheolhyun
  • 169
  • 1
  • 7
  • The problem is not from showingSecond because if i change StateObject var beenf = Benef() by State var benef = "", it work – OuSS Apr 04 '23 at 04:46