The same issue occurs in MacOS 12.1 for my app (written in Swift using Xcode 13.3) during the NSTextView's delegate method:
func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool
{
return askUserIfOkayToEndTextEditing() // Causes a crash the moment the NSAlert is run modally
}
The crash reports on the terminal:
2024-01-03 09:42:35.394910-0700 MyApp[23691:765726] [General] An uncaught exception was raised
2024-01-03 09:42:35.394941-0700 MyApp[23691:765726] [General] -[NSApplication runModalForWindow:] may not be
invoked inside of transaction begin/commit pair, or inside of transaction commit (usually this means it was
invoked inside of a view's -drawRect: method.)
I've not tested this on later systems. What's weird is that putting up the alert used to work and now doesn't, even though I've not updated my MacOS system recently. So it seems some other condition is triggering the crash. Currently a mystery, but it would be nice to know from Apple what the best practice is for putting up an alert such as this as soon as possible to know how to proceed. The problem is that the delegate method seems completely like the right place to put up such an alert. Alas, not.
Topic:
UI Frameworks
SubTopic:
AppKit
Tags: