我正在构建一个程序,该程序启动另一个程序,然后监视它,并在它终止时采取行动。当应用程序启动时,我可以从NSRunningApplication NSWorkspace获得一个实例。
现在,文档声明NSRunningApplication的属性“终止”,这是键值可观察的。我试着实现:
[browserInstance addObserver:self
forKeyPath:@"terminated"
options:NSKeyValueObservingOptionNew
context:NULL];和:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"observeValueForKeyPath");
if ([keyPath isEqual:@"terminated"])
{
NSLog(@"terminated");
}
} 但我从未见过observeValueForKeyPath方法会被绊倒。如果可能的话,有谁知道如何使这件事成功吗?我还没能在网上找到任何具体的例子。
发布于 2011-01-24 20:07:55
你试过keyPath的"isTerminated“吗?
注意,在NSRunningApplication的文档中,属性terminated指定getter isTerminated,而不是默认的getter terminated。(这是有意义的,因为布尔属性"is“或”isn“)
这表明obj-c属性解析中可能存在错误,其中getter的名称用于KVO路径。
发布于 2011-01-23 16:09:00
最后我用了:
NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
// Install the notifications.
[center addObserver:self
selector:@selector(appLaunched:)
name:NSWorkspaceDidLaunchApplicationNotification
object:nil];
[center addObserver:self
selector:@selector(appTerminated:)
name:NSWorkspaceDidTerminateApplicationNotification
object:nil];然后实现appLaunched和appTerminated方法。
发布于 2010-11-09 01:05:47
“是否插入”问题:您已经验证了browserInstance不是nil,对吗?
https://stackoverflow.com/questions/4128002
复制相似问题