0

NSWindowControllerすでに存在するインスタンスの数を確認するにはどうすればよいですか?同じウィンドウコントローラの複数のウィンドウを開いて、内容が異なるようにしたい。

ウィンドウは次のように開かれます。

....
hwc = [[HistogrammWindowController alloc] init];
....

私は既存のコントローラーをチェックすることを知っています:

if (!hwc)
...

しかし、開いている複数のウィンドウコントローラーの数を知る必要があります。それはどのように見えますか?

ありがとう

4

1 に答える 1

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];
}
于 2013-03-12T21:42:53.143 に答える