Objective c 检查现有的多个实例
如何检查NSWindowController已经存在多少个实例? 我想打开同一窗口控制器的多个窗口,显示不同的内容 窗口是这样打开的:Objective c 检查现有的多个实例,objective-c,macos,multiple-instances,nswindowcontroller,Objective C,Macos,Multiple Instances,Nswindowcontroller,如何检查NSWindowController已经存在多少个实例? 我想打开同一窗口控制器的多个窗口,显示不同的内容 窗口是这样打开的: .... hwc = [[HistogrammWindowController alloc] init]; .... 我知道要检查已存在的控制器: if (!hwc) ... 但我需要知道多个打开的窗口控制器的数量。那会是什么样子 感谢您,您可以跟踪NSSet中的每个窗口实例,除非您需要访问它们的创建顺序,在这种情况下,请使用NSArray。当一个窗口出现时
....
hwc = [[HistogrammWindowController alloc] init];
....
我知道要检查已存在的控制器:
if (!hwc)
...
但我需要知道多个打开的窗口控制器的数量。那会是什么样子
感谢您,您可以跟踪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];
}
你好,科达菲,这听起来很简单。你能告诉我你能把不同的窗口和同一个控制器区分开来吗?它们都使用相同的hwc实例打开。那么,NSSet中实际添加了什么?谢谢,我明白了。我对这个行业还很陌生。。。你能用密码给我一个提示吗?我从未使用过标识符。谢谢你。什么。。。!这看起来非常漂亮,不像我一开始想的那么容易。我很高兴能提出问题并找到这么好的答案。谢谢,祝你好运!