以编程方式关闭NSDocument的正确方法

时间:2013-02-25 21:17:03

标签: macos cocoa nsdocument

我有一个NSDocument的子类,每当我使用[self close]时都会崩溃,以避免新文档的默认“保存”对话框:

2013-02-25 15:23:06.338 MyApp[692:403] An uncaught exception was raised
2013-02-25 15:23:06.338 MyApp[692:403] NSWindow: -_newFirstResponderAfterResigining is not a valid message outside of a responder's implementation of -resignFirstResponder.
2013-02-25 15:23:06.356 MyApp[692:403] (
0   CoreFoundation                      0x00007fff8b2cdf56 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff88c00d5e objc_exception_throw + 43
2   CoreFoundation                      0x00007fff8b2cdd8a +[NSException raise:format:arguments:] + 106
3   CoreFoundation                      0x00007fff8b2cdd14 +[NSException raise:format:] + 116
4   AppKit                              0x00007fff858e0c34 -[NSWindow _newFirstResponderAfterResigning] + 164
5   AppKit                              0x00007fff85882c8a -[NSTextView(NSSharing) resignFirstResponder] + 54
6   AppKit                              0x00007fff8536dd79 -[NSWindow makeFirstResponder:] + 429
7   AppKit                              0x00007fff85488dfb -[NSWindow dealloc] + 802
8   AppKit                              0x00007fff8528b955 -[NSWindow release] + 535
9   AppKit                              0x00007fff858b541c __destroy_helper_block_4 + 26
10  libsystem_blocks.dylib              0x00007fff8702b174 _Block_release + 181
11  libdispatch.dylib                   0x00007fff84ab28f2 _dispatch_main_queue_callback_4CF + 308
12  CoreFoundation                      0x00007fff8b262e7c __CFRunLoopRun + 1724
13  CoreFoundation                      0x00007fff8b262486 CFRunLoopRunSpecific + 230
14  HIToolbox                           0x00007fff8d64e2bf RunCurrentEventLoopInMode + 277
15  HIToolbox                           0x00007fff8d65556d ReceiveNextEventCommon + 355
16  HIToolbox                           0x00007fff8d6553fa BlockUntilNextEventMatchingListInMode + 62
17  AppKit                              0x00007fff85252779 _DPSNextEvent + 659
18  AppKit                              0x00007fff8525207d -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
19  AppKit                              0x00007fff8524e9b9 -[NSApplication run] + 470
20  AppKit                              0x00007fff854caeac NSApplicationMain + 867
21  MyApp                                0x00000001059098d2 main + 34
22  MyApp                                0x00000001059098a4 start + 52
23  ???                                 0x0000000000000003 0x0 + 3
)

有没有人见过这个或者有人可以建议一个更好/更合适的方法来关闭NSDocument而不显示保存对话框?

1 个答案:

答案 0 :(得分:2)

因此,根据您的后续评论判断,您要做的是在特定情况下取消标准关闭确认表。一些方法:

  • 采用自动保存,远离手动保存文档的古老范式
  • 覆盖-[NSDocument canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:],以便它告诉代表“是的,你可以关闭”,而不是展示一张表
  • 发布信息后,请致电[document updateChangeCount: NSChangeCleared]通知文档您认为该信息已保存