我使用- (void)windowControllerDidLoadNib:(NSWindowController *)aController
查看文档何时加载到基于文档的应用程序中。但是,我应该使用什么方法来查看文档何时关闭?我想将文本框的内容保存到NSUserDefaults但我无法找到文档关闭时调用的方法。我搜索了网络,并通过在xcode中显示为提示的方法,但没有运气!任何帮助赞赏!
感谢
答案 0 :(得分:4)
我观察NSApplicationWillTerminateNotification
并覆盖[NSDocument close]
方法以执行文档清理(当应用终止时不会调用[NSDocument close]
!)
MyDocument.h:
@interface MyDocument : NSDocument
{
BOOL _cleanedUp; // BOOL to avoid over-cleaning up
...
}
@end
MyDocument.m:
// Private Methods
@implementation MyDocument ()
- (void)_cleanup;
@end
@implementation MyDocument
- (id)init
{
self = [super init];
if (self != nil)
{
_cleanedUp = NO;
// Observe NSApplication close notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_cleanup)
name:NSApplicationWillTerminateNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// I'm using ARC so there is nothing else to do here
}
- (void)_cleanup
{
if (!_cleanedUp)
{
_cleanedUp = YES;
logdbg(@"Cleaning-up");
// Do my clean-up
}
}
- (void)close
{
logdbg(@"Closing");
[self _cleanup];
[super close];
}