# dismiss

[SwiftUI](https://lochiwei.gitbook.io/ios/swiftui) ⟩ [view](https://lochiwei.gitbook.io/ios/swiftui/view) ⟩ [environment](https://lochiwei.gitbook.io/ios/swiftui/view/environment) ⟩ [value](https://lochiwei.gitbook.io/ios/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](https://lochiwei.gitbook.io/ios/swiftui/view/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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swiftui/view/environment/value/dismiss.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
