Trouble with getting a sheet to slide into view

I am putting an app together that consists of several buttons. To help clean code I created a view for the buttons and did all the design on them there. I then created a background view to lay out the buttons that will be seen when contentView loads. I created a State variable in my background view and added my .sheet code (see below) but when I click on it I do not get a sheet sliding in. I am not seeing what I am missing so any guidance would be greatly appreciated. The view the sheet should display is called Wetlab104. So when I click the button I would like the sheet for that room to appear.

///swift
struct BackgroundView: View {
@State private var Wetlab104IsShowing = false
@State private var rm105IsShowing = false
var body: some View {
VStack {
HStack {
Button {
Wetlab104IsShowing = true
} label: {
ButtonView(text: “Rm 104”)
}
.sheet(isPresented: $Wetlab104IsShowing) {
Wetlab104()
}
Spacer()
Button {
rm105IsShowing = true
} label: {
ButtonView(text: “Rm 105”)
}
.sheet(isPresented: $rm105IsShowing) {
Wetlab105()
}
}
HStack {
Button {
// show room 107
} label: {
ButtonView(text: “Rm 107”)
}
Spacer()
Button {
// show room 108
} label: {
ButtonView(text: “Rm 108”)
}
}
HStack {
Button {
// show room 118
} label: {
ButtonView(text: “Rm 118”)
}
Spacer()
Button {
// show room 120
} label: {
ButtonView(text: “Rm 120”)
}
}
Button {
// show room 120
} label: {
ButtonView(text: “Q-Lab”)
}
}
}
}
struct BackgroundView_Previews: PreviewProvider {
@Binding var rm104IsShowing: Bool
static var previews: some View {
BackgroundView()
BackgroundView()
.preferredColorScheme(.dark)
.previewDevice(“iPhone 15”)
}
}
///

Hi @seanec - would you be kind enough to repost/edit your code? It’s broken up and difficult to read. Try using the three backticks, followed by swift `swift (without the spaces) and put the code after, then three backticks to close the code. Thanks!

Thanks Roberto for the suggestion. Hopefully it’s more readable now. I appreciate any suggestions you can provide.

You directly added the .sheet() statement to the button. Try adding it to the main view instead…

Thanks for the support. I tried as you suggested and it still will not slide a sheet in. I thought it did have to be attached to the button in order for it to work when the button is tapped. I also tried to put it in a ZStack to see if that would help but to no avail. Still stuck on this one

I finally got the sheet to slide in. Unfortunately I had to remove my ButtonView() which had all the styling for the button and then add the styling to each button individually. In an app with many buttons that makes for a lot of repeat code. I was hoping to style the buttons once in a separate view then bring that in for each button. Is this possible. Perhaps I should not be using a view to do this but something else?

Yes, you want to do that. It will keep the code cleaner and DRY (Don’t Repeat Yourself).

This topic was automatically closed after 166 days. New replies are no longer allowed.