我使用xib文件创建了一个名为BOHeaderView的Custom UIView。我想将这个BOHeaderView加载到其他UIViewController的xibs中。
我尝试在一个ViewController nib文件中添加UIView并将其类型更改为customView。但我无法加载自定义视图。下面是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];
}
答案 0 :(得分:0)
NSBundle
的那一行应该在你要加载nib的类中。你应该写那个班:
CustomView *customView = [[NSBundle mainBundle] loadNibNamed:@"BOHeaderView" owner:self options:nil].lastObject;
它会返回NSArray
,这就是您需要lastObjct
答案 1 :(得分:0)
你可以这样做:
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];
编辑:
意味着你想说:Using a custom UIView from multiple view controllers
我遇到了同样的问题,我用这种方式解决了这个问题:
我在buttonClick
上编写了以下代码。
RatingView *rateView = [[RatingView alloc] initWithNibName:@"RatingView" bundle:[NSBundle mainBundle]];
rateView.view.frame = CGRectMake(0,45,1024, 648);
[self.view addSubview:rateView.view];
快乐编码!!