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];
}