📦dismiss

SwiftUIviewenvironmentvalue ⟩ dismiss

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

  • dismiss a modal presentation, like a sheet or a popover.

  • pop the current view from a NavigationStack.

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

Was this helpful?