2

Please forgive me as this question has been asked 100 times on here (and I promise that I've read every thread!), but I simply can't figure out how to correctly load a NIB file into a UIView. I tried two approaches (seen below), though neither seemed to work for me. I have a UINavController/UIViewController setup and I'm trying to load a subclassed UIView into a small area on one of these UIViewControllers.

This answer seemed reasonable, but i can't seem to get it to work: iPhone - Load a UIView from a nib file?

// ViewController.m 
localAssetUpdater = [[LocalAssetUpdater alloc] initWithFrame:[self.view frame]]; 

// LocalAssetUpdater.m    
- (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
        [[NSBundle mainBundle] loadNibNamed:@"LocalAssetUpdater" owner:self options:nil];

        [self reset];
        [self beginLoad];
    }
    return self;
}


 //OR: 


// ViewController.m
localAssetUpdater = [LocalAssetUpdater myView];


// LocalAssetUpdater.m 
+ (LocalAssetUpdater *) myView
{
    NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
    return [array objectAtIndex:0]; // assume that MyView is the only object in the xib
}
Community
  • 1
  • 1
Brian A Bird
  • 378
  • 2
  • 18

1 Answers1

1
@implementation MyView

+ (MyView*) myView
{
  NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
  for (id object in array) {
    if ([object isKindOfClass:[YourClassName class]])
        self = (YourClassName*)object;
}   
}

@end
Jordan
  • 21,746
  • 10
  • 51
  • 63