我有一个基于文档的应用程序,当应用程序关闭时,我需要在网络浏览器中加载一个网址。它工作正常,只是 NSDocument
在加载此页面之前关闭。
我需要它等待200 ms
,然后关闭文档。
我找到了 NSTerminateLater
但那是指应用程序,而不是文档。我该怎么做?
这就是我现在所拥有的:
- (id)init
{
self = [super init];
if (self) {
_statssent = NO;
// Observe NSApplication close notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_sendstats)
name:NSApplicationWillTerminateNotification
object:nil];
}
return self;
}
- (void)_sendstats
{
if (!_statssent)
{
_statssent = YES;
if (hasuploaded == 1)
{
[self updatestatsUploads:0 progloads:1];
}
}
}
- (void)close
{
[self _sendstats];
[super close];
}
最佳答案
就在关闭文档之前,您可以发出一条通知,您的应用程序委托(delegate)可以注册为观察员。
当您的应用程序委托(delegate)收到通知(可以传达您需要打开的 URL)时,可以调用应用程序委托(delegate)上的方法来为您打开该 URL。
您可以通过使用每个 Cocoa 应用程序附带的 NSNotificationCenter
实例(更准确地说,它是一个单例)来完成此操作。您的文档将发出如下通知:
NSDictionary *myUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.apple.com", @"MyURL", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotificationName" object:self userInfo:myUserInfo];
在您的应用程序委托(delegate)中,可能在您的 -awakeFromNib
方法中,您将使用如下内容:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myURLOpenerMethod:) name:@"MyNotificationName" object:nil];
在应用程序委托(delegate)中的某个位置,您可以像这样定义 URL 打开器:
- (void)myURLOpenerMethod:(NSNotification *)notification
{
NSString *urlString = [[notification userInfo] objectForKey:@"MyURL"];
// use 'urlString' to open your URL here
}
您不想想尝试使用延迟来获得您想要的东西。我向你保证:疯狂就在于此。
关于objective-c - 延迟文档关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166135/