I’ve two Modal/Popover .sheet
‘s I wish to present primarily based on which button is pressed by a consumer. I’ve setup an enum
with the totally different selections and set a default alternative.
Anticipated behaviour:
When the consumer selects any alternative, the suitable sheet is displayed. When the consumer THEN selects the opposite alternative, it additionally exhibits the right sheet.
Noticed behaviour:
Within the instance beneath, when the consumer first picks the second alternative, the primary sheet is proven and can proceed to point out till the consumer selects the primary sheet, then it is going to begin to change.
Debug printing exhibits that the @State
variable is altering, nevertheless, the sheet presentation doesn’t observe this variation and exhibits the sheets as described above. Any ideas?
import SwiftUI
//MARK: important view:
struct ContentView: View {
//assemble enum to determine which sheet to current:
enum ActiveSheet {
case sheetA, sheetB
}
//setup wanted vars and set default sheet to point out:
@State var activeSheet = ActiveSheet.sheetA //units default sheet to Sheet A
@State var showSheet = false
var physique: some View {
VStack{
Button(motion: {
self.activeSheet = .sheetA //set option to Sheet A on button press
print(self.activeSheet) //debug print present activeSheet worth
self.showSheet.toggle() //set off sheet
}) {
Textual content("Present Sheet A")
}
Button(motion: {
self.activeSheet = .sheetB //set option to Sheet B on button press
print(self.activeSheet) //debug print present activeSheet worth
self.showSheet.toggle() //set off sheet
}) {
Textual content("Present Sheet B")
}
}
//sheet selecting view to show primarily based on chosen enum worth:
.sheet(isPresented: $showSheet) {
change self.activeSheet {
case .sheetA:
SheetA() //current sheet A
case .sheetB:
SheetB() //current sheet B
}
}
}
}
//MARK: ancillary sheets:
struct SheetA: View {
var physique: some View {
Textual content("I'm sheet A")
.padding()
}
}
struct SheetB: View {
var physique: some View {
Textual content("I'm sheet B")
.padding()
}
}