BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

Can't dismiss sheet

Forums > SwiftUI

I have let say view A that is a sheet. From view A you can pull up another sheet, call it sheet B. I can dismiss sheet B just fine. But when I then try to dismiss sheet A. It wont dismiss. Does anyone else have this issue, if so how do you solve it.

Just for info, Sheet A is showing an edit contact view. From that view their is a VStack with a button in it that displays customs events. When you tap the button on the VStack it brings up sheet B. As I said I can dismiss that view just fine after creating the event. But sheet A will not dismiss.

I hope that is clear on what I is going on. I just cant understand why sheet A wont dismiss if I add event and then dismiss sheet B.

Thanks,

   

Let's say, instead of describing View A and hiz friend View B, you just show some code. This would help us see what concepts you've mastered and where you might be having a little trouble.

Here's some code showing a simple HomeView. Tap the button and SheetOneView slides in as a sheet. On SheetOneView is another button that slides in SheetTwoView.

If you swipe down on SheetTwoView, you're back to SheetOneView. Again, swipe down and you're back to the HomeView.

//  Multi_Sheet_Experiment
//
//  Created by Obelix on 4/19/22.
import SwiftUI

struct HomeView: View {
    @State private var showSheet1 = false  // Toggle Sheet One
    var body: some View {
        VStack {
            Text("Home View").font(.largeTitle).foregroundColor(.cyan).bold().padding()
            Button { showSheet1.toggle() }
                label: { Text(showSheet1 ? "Hide Sheet 1" : "Show Sheet 1") }
        }.sheet(isPresented: $showSheet1) { SheetOneView() }
    }
}

struct SheetOneView : View {
    @State private var showSheet2 = false
    var body: some View {
        VStack {
            Text("Sheet One").font(.largeTitle).foregroundColor(.blue).bold().padding()
            Button { showSheet2.toggle() }
                label: { Text(showSheet2 ? "Hide Sheet 2" : "Show Sheet 2") }
        }.sheet(isPresented: $showSheet2) { SheetTwoView() }
    }
}

struct SheetTwoView : View {
    var body: some View {
        Text("Sheet 2").font(.largeTitle).foregroundColor(.indigo).bold().padding()
        Text("(Swipe down to dimiss.)").font(.caption)
    }
}

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.