---
title: "stopModal(withCode:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsapplication/stopmodal(withcode:)"
---

# stopModal(withCode:)

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

## Declaration

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

## Parameters

- `returnCode`: The result code you want returned from the doc://com.apple.appkit/documentation/AppKit/NSApplication/runModal(for:) or doc://com.apple.appkit/documentation/AppKit/NSApplication/runModalSession(_:) method. The meaning of this result code is up to you.

## Discussion

Discussion This method should always be paired with a previous invocation of runModal(for:) or beginModalSession(for:). When 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:) loop outside of an event callback, such as from within a method repeatedly invoked by an Timer object or a method running in a different thread.

## See Also

### Running a Modal Window

- [runModal(for:)](appkit/nsapplication/runmodal(for:).md)
- [stopModal()](appkit/nsapplication/stopmodal().md)
- [abortModal()](appkit/nsapplication/abortmodal().md)
- [beginModalSession(for:)](appkit/nsapplication/beginmodalsession(for:).md)
- [runModalSession(_:)](appkit/nsapplication/runmodalsession(_:).md)
- [modalWindow](appkit/nsapplication/modalwindow.md)
- [NSApplication.ModalResponse](appkit/nsapplication/modalresponse.md)
- [NSApplication.ModalSession](appkit/nsapplication/modalsession.md)
