Created
November 30, 2011 01:57
-
-
Save AtomicCat/1407621 to your computer and use it in GitHub Desktop.
UIView (NibLoadingExtension)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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