1 min readOct 10, 2020
Kind of a long way around the barn.
If I needed to to have multiple buttons and each one needed to present a different sheet, and if I didn’t want all of the boilerplate I’d just make a view for that.
struct ButtonSheetView<Content:View>: View { let title: String
let content: () -> Content @State private var presentSheet = false init(_ title: String, _ content: @escaping () -> Content) {
self.title = title
self.content = content
} var body: some View {
return Button(title) {
self.presentSheet = true
}
.sheet(isPresented: $presentSheet) {
self.content()
}
}
}
With usage like..
ButtonSheetView("Test Button 1") {
Text("This is a test!")
}
ButtonSheetView("Test Button 2") {
Text("This is another test!")
}
Each button has its own presentation state and each button has its own sheet, and using them is just like using a NavigationLink.
No fuss, no muss.
Remember that SwiftUI’s primary emphasis is on decomposition, and making custom views carries little to performance penalties.