Reputation: 1356
I am failing miserably trying to wrap my head around how to use .sheet(item:content:)
. I know that there is a lot of information here and on other platforms, but I just cannot get it to work...
Here's an abstraction of the view I'd like to have – and I don't know what I am doing wrong here:
import SwiftUI
enum SheetView: String, Identifiable {
var id: Self { self }
case sheetA = "Sheet A"
case sheetB = "Sheet B"
}
struct SheetViewTest: View {
@State private var showSheet: SheetView? = nil
var body: some View {
Form {
Button(action: {
showSheet = .sheetA
print("Button \(showSheet?.rawValue) tapped…")
}, label: {
Text(SheetView.sheetA.rawValue)
})
Button(action: {
showSheet = .sheetB
print("Button \(showSheet?.rawValue) tapped…")
}, label: {
Text(SheetView.sheetB.rawValue)
})
}
.sheet(item: $showSheet) { sheet -> View in
Text(sheet.rawValue)
}
}
}
struct SheetViewTest_Previews: PreviewProvider {
static var previews: some View {
SheetViewTest()
}
}
The error I am on the body getting is as follows:
Value of protocol type 'View' cannot conform to 'View'; only struct/enum/class types can conform to protocols
, which is due to the .sheet
modifier cause when I comment it out, the view works just fine...
Upvotes: 3
Views: 887
Reputation: 257523
Just remove closure return type (which is inferred automatically from returned content), i.e.
.sheet(item: $showSheet) { sheet in // << here !!
Text(sheet.rawValue)
}
Upvotes: 5