<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSApplication/stopModal(withCode:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSApplication(im)stopModalWithCode:"
  },
  "title" : "stopModal(withCode:)"
}
-->

# stopModal(withCode:)

Stops a modal event loop, allowing you to return a custom result code.

```
func stopModal(withCode returnCode: NSApplication.ModalResponse)
```

## Parameters

`returnCode`

The result code you want returned from the [`runModal(for:)`](/documentation/AppKit/NSApplication/runModal(for:)) or [`runModalSession(_:)`](/documentation/AppKit/NSApplication/runModalSession(_:)) method. The meaning of this result code is up to you.

## Discussion

This method should always be paired with a previous invocation of [`runModal(for:)`](/documentation/AppKit/NSApplication/runModal(for:)) or [`beginModalSession(for:)`](/documentation/AppKit/NSApplication/beginModalSession(for:)). When [`runModal(for:)`](/documentation/AppKit/NSApplication/runModal(for:)) is stopped with this method, it returns the given `returnCode`. In macOS 10.9 and later, you can use this method to stop a [`runModal(for:)`](/documentation/AppKit/NSApplication/runModal(for:)) loop outside of an event callback, such as from within a method repeatedly invoked by an <doc://com.apple.documentation/documentation/Foundation/Timer> object or a method running in a different thread.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)