Skip to content

Instantly share code, notes, and snippets.

@AtomicCat
Created November 30, 2011 01:57
Show Gist options
  • Select an option

  • Save AtomicCat/1407621 to your computer and use it in GitHub Desktop.

Select an option

Save AtomicCat/1407621 to your computer and use it in GitHub Desktop.
UIView (NibLoadingExtension)
@implementation UIView (NibLoadingExtension)
+ (id)viewFromNib
{
static NSMutableDictionary *caches = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
caches = [[NSMutableDictionary dictionary] retain];
});
Class class = [self class];
NSString *className = NSStringFromClass(class);
UINib *nib = [caches objectForKey:className];
if (nib == nil)
{
nib = [UINib nibWithNibName:className bundle:[NSBundle mainBundle]];
[caches setObject:nib forKey:className];
}
NSArray *topLevelItems = [nib instantiateWithOwner:nil options:nil];
id view = nil;
for (id object in topLevelItems)
{
if ([object isKindOfClass:class])
{
view = object;
break;
}
}
return view;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment