Last Updated: February 25, 2016
·
405
· vsching

Load Nib File Easily

+ (id)viewFromNibFileOfNibNameEqualClassNameWithOwner:(id)owner{
    Class cls = [self class];
    NSString *className = NSStringFromClass(cls);

    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:className owner:owner options:NULL];
    NSEnumerator *nibEnumerator = [nibContents objectEnumerator];

    NSObject* nibItem = nil;
    id  object = nil;

    while ((nibItem = [nibEnumerator nextObject]) != nil) {
        if ([nibItem isKindOfClass:cls]) {
            object = nibItem;
        break; 
    }
    }
    return object;
}

Sample use

if (cell == nil) {
        cell = [BranchesAtmDetailTableCell viewFromNibFileOfNibNameEqualClassNameWithOwner:self];
    }