2011-04-12 96 views
2

請原諒我,因爲這個問題在這裏已經被問了100次(並且我保證我已經讀過每一個線程!),但我根本無法弄清楚如何正確加載NIB文件導入UIView。我嘗試了兩種方法(見下文),但似乎都不適合我。我有一個UINavController/UIViewController設置,我試圖加載一個subclassed UIView到這些UIViewControllers之一的一個小區域。將NIB文件加載到UIView中時遇到問題

這個回答似乎合理,但我似乎無法得到它的工作: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 
} 

回答

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