In the "Keep Going with Apps" Playground under the "Add and delete creatures" section, you are guided through how to "Dismiss the editor when you tap Add". Just one problem: it doesn't work. I'm on Monterey 12.6 (21G115) with Playgrounds 4.1 (1676.15). This is version 1.0.0 (December 15, 2021) Swift 5.5 edition of the playground. (It doesn't work with the previous version of MacOS I had installed, either.)
The simplified version of the code:
import SwiftUI
import Guide
struct CreatureEditor: View {
@EnvironmentObject var zoo: CreatureZoo
@Environment(\.dismiss) private var dismiss
@State var newCreature = Creature(name: "", emoji: "")
var body: some View {
SPCAssessableGroup(view: self) {
VStack(alignment: .leading) {
Form {
// omitted
}
}
.toolbar {
ToolbarItem {
Button("Add") {
zoo.creatures.append(newCreature)
dismiss()
}
}
}
}
}
}
It's easy enough to confirm the closure is being called. The view is contained inside the ContentView, which in turn is contained inside a NavigationView in MyApp.
The documentation of the dismiss property notes that "The dismiss action has no effect on a view that isn’t currently presented." So I pulled in @Environment(\.isPresented) private var isPresented and confirmed it's set to true.
Any thoughts? Until this point, this playground has been an excellent way to learn SwiftUI.
Update: The now-deprecated presentationMode.wrappedValue.dismiss() also fails to dismiss, so it's quite possible something is structurally wrong with the playground app.