# dismiss

[SwiftUI](../../..) ⟩ [view](../..) ⟩ [environment](..) ⟩ [value]() ⟩ dismiss

{% hint style="success" %}
An [action](https://developer.apple.com/documentation/swiftui/dismissaction) that <mark style="color:yellow;">dismisses</mark> the <mark style="color:yellow;">current presentation</mark>.

```swift
@Environment(\.dismiss) var dismiss
```

Use this [environment value]() to get the [`DismissAction`](https://developer.apple.com/documentation/swiftui/dismissaction) <mark style="color:yellow;">instance</mark> for the <mark style="color:yellow;">current</mark> [`Environment`](https://developer.apple.com/documentation/swiftui/environment). Then <mark style="color:yellow;">call the instance</mark> (<mark style="color:purple;">`dismiss()`</mark>) to <mark style="color:yellow;">perform the dismissal</mark>.&#x20;
{% endhint %}

You call the instance directly because it defines a [`callAsFunction()`](https://developer.apple.com/documentation/swiftui/dismissaction/callasfunction\(\)) method that Swift calls when you call the instance. You can use this action to:

* <mark style="color:yellow;">dismiss</mark> a <mark style="color:orange;">modal</mark> <mark style="color:yellow;">presentation</mark>, like a <mark style="color:orange;">sheet</mark> or a <mark style="color:orange;">popover</mark>.
* <mark style="color:yellow;">pop the current view</mark> from a [`NavigationStack`](https://developer.apple.com/documentation/swiftui/navigationstack).

{% tabs %}
{% tab title="💾 程式" %}
```swift
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
    }
}
```
{% endtab %}

{% tab title="📘 手冊" %}
* [SwiftUI](https://developer.apple.com/documentation/swiftui) ⟩ [Environment values](https://developer.apple.com/documentation/swiftui/environment-values) ⟩ [EnvironmentValues](https://developer.apple.com/documentation/swiftui/environmentvalues) ⟩ [dismiss](https://developer.apple.com/documentation/swiftui/environmentvalues/dismiss)&#x20;
{% endtab %}

{% tab title="📗 參考" %}
* Perplexity ⟩ [@Environment(.dismiss) 的作用是什麼？](https://www.perplexity.ai/search/zai-swiftui-zhong-environment-eAFBpGIOSIyo3XgqHAOc.Q)&#x20;
{% endtab %}
{% endtabs %}
