如何检查某个NSWindowController已经存在多少个实例?我想打开同一个窗口控制器的多个窗口,显示不同的内容。
窗口是这样打开的:
....
hwc = [[HistogrammWindowController alloc] init];
....我知道要检查已经存在的控制器:
if (!hwc)
...但我需要知道多个打开的窗口控制器的数量。那会是什么样子呢?
谢谢
发布于 2013-03-13 05:42:53
您可以跟踪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];
}https://stackoverflow.com/questions/15372679
复制相似问题