首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查是否存在多个实例

检查是否存在多个实例
EN

Stack Overflow用户
提问于 2013-03-13 05:37:51
回答 1查看 111关注 0票数 0

如何检查某个NSWindowController已经存在多少个实例?我想打开同一个窗口控制器的多个窗口,显示不同的内容。

窗口是这样打开的:

代码语言:javascript
复制
....
hwc = [[HistogrammWindowController alloc] init];
....

我知道要检查已经存在的控制器:

代码语言:javascript
复制
if (!hwc)
...

但我需要知道多个打开的窗口控制器的数量。那会是什么样子呢?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-13 05:42:53

您可以跟踪NSSet中的每个窗口实例,除非您需要访问它们的创建顺序,在这种情况下,请使用NSArray。当一个窗口出现时,将它添加到给定的集合中,当它被关闭时,删除它。作为一个额外的好处,当应用程序退出时,您可以通过迭代集合来关闭每个打开的窗口。

也许有点像这样:

代码语言:javascript
复制
- (IBAction)openNewWindow:(id)sender {
    HistogrammWindowController *hwc = [[HistogrammWindowController alloc] init];
    hwc.uniqueIdentifier = self.uniqueIdentifier;

    //To distinguish the instances from each other, keep track of
    //a dictionary of window controllers for UUID keys.  You can also
    //store the UUID generated in an array if you want to close a window 
    //created at a specific order.
    self.windowControllers[hwc.uniqueIdentifier] = hwc;
}

- (NSString*)uniqueIdentifier {
    CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
    NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
    CFRelease(uuidObject);
    return uuidStr;
}

- (IBAction)removeWindowControllerWithUUID:(NSString*)uuid {
    NSWindowController *ctr = self.windowControllers[uuid];
    [ctr close];
    [self.windowControllers removeObjectForKey:uuid];
}

- (NSUInteger)countOfOpenControllers {
    return [self.windowControllers count];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15372679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档