Figuring out the file's owner class
Figuring out the file's owner class
- Subject: Figuring out the file's owner class
- From: "Mills, Steve" <email@hidden>
- Date: Fri, 23 May 2014 14:51:21 +0000
- Thread-topic: Figuring out the file's owner class
We have some generic window loading code that must instantiate the window controller first, then calls initWithWindowNibName: on that. This made it impossible to provide a subclass of our generic window controller subclass in the xib, but now we need to do this. Since NSNib doesn't have a method or property for the file's owner, the best way I know of to get this is to look for the window in the nib (see function below), gets its controller, get the controller's class, then release the window so we can create things in our "normal" way.
If there some better way to get the file's owner class?
id LoadObjectFromNibNamed(NSString* const nibName, const Class ofClass)
{
NSNib* nib = [[[NSNib alloc] initWithNibNamed:nibName bundle:nil] autorelease];
NSArray* topLevelObjects = nil;
id result = nil;
if([nib instantiateNibWithOwner:nil topLevelObjects:&topLevelObjects]) {
for(id topLevelObject in topLevelObjects) {
if([topLevelObject isKindOfClass:ofClass]) {
result = topLevelObject;
break;
}
}
for(id obj in topLevelObjects) {
if(obj != result)
[obj release];
}
}
return result; // Caller will own this.
}
--
Steve Mills
office: 952-818-3871
home: 952-401-6255
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden