如何查看已存在NSWindowController
的实例数?
我想打开显示不同内容的同一个窗口控制器的多个窗口。
窗口以这种方式打开:
....
hwc = [[HistogrammWindowController alloc] init];
....
我知道检查现有的控制器:
if (!hwc)
...
但我需要知道多个打开的窗口控制器的数量。这看起来怎么样?
由于
答案 0 :(得分:1)
您可以跟踪NSSet
中的每个窗口实例,除非您需要访问创建它们的顺序,在这种情况下使用NSArray
。当窗口出现时,将其添加到给定的集合中,当它关闭时,将其删除。另外一个好处是,当应用程序退出时,您可以通过遍历集合来关闭每个打开的窗口。
也许有点像这样:
- (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];
}