📦dismiss

SwiftUIviewenvironmentvalue ⟩ dismiss

circle-check

You call the instance directly because it defines a callAsFunction()arrow-up-right method that Swift calls when you call the instance. You can use this action to:

struct CreatureEditor: View {
    
    @State var newCreature = Creature(name: "", emoji: "") // new creature to add to data
    @EnvironmentObject var data : Zoo                      // data model
    
    // ⭐️⭐️⭐️ allow the app to dismiss the current view
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        VStack(alignment: .leading) {
        
            Form { ... }    // for editing new creature
            
            Button("Add") { 
                // add new item to data
                data.creatures.append(newCreature)
                
                // ⭐️ dismiss current view
                dismiss()
            }
            
        }// VStack
    }
}

Last updated