0
votes

I created a Custom UIView called BOHeaderView using xib file. I want this BOHeaderView to be loaded in other UIViewController' xibs.

I tried by adding a UIView in one ViewController nib file and change its type to customView. But I am not able to load the custom view. Below is the initialization code of customView.

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Custom initialization
        [[NSBundle mainBundle] loadNibNamed:@"BOHeaderView" owner:self options:nil];
    }
    return self;
}

- (void) awakeFromNib
{
    [super awakeFromNib];

    // commenters report the next line causes infinite recursion, so removing it
    [self customizeHeadView];
}
2

2 Answers

0
votes

That row with the NSBundle should be in the class in which you want to load the nib. You should write in that class:

CustomView *customView = [[NSBundle mainBundle] loadNibNamed:@"BOHeaderView" owner:self options:nil].lastObject;

It returns an NSArray, that is why you need the lastObjct

0
votes

You can do it like this :

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"BOHeaderView" 
                                              owner:[[BOHeaderView alloc] init]
                                            options:nil];
BOHeaderView *infoView = [nibViews objectAtIndex:0];
infoView.frame = CGRectMake( put your rect here );
[self addSubview:infoView];

EDIT :

Means you are trying to say : Using a custom UIView from multiple view controllers

I had the same problem and I solved it in this way :

I wrote the below code on buttonClick.

 RatingView *rateView = [[RatingView alloc] initWithNibName:@"RatingView" bundle:[NSBundle mainBundle]];
 rateView.view.frame = CGRectMake(0,45,1024, 648);
 [self.view addSubview:rateView.view];

Happy Coding !!