Post

Replies

Boosts

Views

Activity

ObservableObject with Binding cause sheet crash SwiftUI
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 benef = Benef() var body: some Scene { WindowGroup { ContentView(testt: $benef.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>."_ PS: if I change @StateObject var benef = Benef() by @State var benef = "", it work
0
0
387
Apr ’23