Skip to content

Instantly share code, notes, and snippets.

@kaspermunck
Created May 7, 2014 20:17
Show Gist options
  • Select an option

  • Save kaspermunck/1afd3ca724076244a40e to your computer and use it in GitHub Desktop.

Select an option

Save kaspermunck/1afd3ca724076244a40e to your computer and use it in GitHub Desktop.
An example of how loading a nib file with a custom view can be done
// UIView+KHMNibLoading.h
@interface UIView (KHMNibLoading)
+ (instancetype)viewFromNib;
@end
// UIView+KHMNibLoading.m
+ (instancetype)viewFromNib {
NSString *className = NSStringFromClass([self class]);
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:className owner:self options:nil];
id view = nil;
for (id object in bundle) {
if ([object isKindOfClass:[self class]]) {
view = object;
}
}
NSAssert(view != nil, @"Failed attempt to load %@ from nib", className);
return view;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment