Skip to content

Instantly share code, notes, and snippets.

@sbrocket
Created September 14, 2011 00:37
Show Gist options
  • Select an option

  • Save sbrocket/1215569 to your computer and use it in GitHub Desktop.

Select an option

Save sbrocket/1215569 to your computer and use it in GitHub Desktop.
// Uses a private internal subclass to allow for loading the view from within a NIB.
// The subclass allows us to break the initialization loop that would otherwise occur.
@interface SomeMyView__ : SomeMyView {
}
@end
@implementation SomeMyView
+ (SomeMyView*)loadFromNib {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"SomeMyView" owner:nil options:nil];
for (id obj in objects) {
if ([obj isKindOfClass:[SomeMyView__ class]]) {
return obj;
}
}
assertLog(@"Couldn't find SomeMyView__ in objects instantiated from NIB!");
return nil;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
if ([[self class] isEqual:[SomeMyView class]]) {
CGRect frame = self.frame;
[self release];
self = [[SomeMyView loadFromNib] retain];
self.frame = frame;
}
}
return self;
}
@end
@implementation SomeMyView__
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment