我使用CustomView.xib文件创建了CustomView:UIView。 现在我想通过拖动视图到另一个XIB(例如:UIViewController.xib)并选择class:customView来使用它。在另一个XIB中指定类时如何使用CustomView.xib加载CustomView?
当我初始化CustomView和addSubView到anotherView可以装入成功:
- (void)viewDidLoad{
//Success load NIB
CustomView *aView = [[CustomView alloc] initWithFrame:CGRectMake(40, 250, 100, 100)];
[self.view addSubview:aView];
}
//CustomView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"INIT");
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
[[nib objectAtIndex:0] setFrame:frame];
self = [nib objectAtIndex:0];
}
return self;
}
在这种情况下通过绘制到另一个XIB重用CustomCell然后指定类作为CustomView。我知道awakeFromNib被调用,但不知道如何加载CustomView.xib。 该怎么办?
*编辑:
的initWithCoder也被称为时指定类,但它创建一个循环和崩溃与loadNibNamed。为什么?
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"Coder");
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QSKey" owner:nil options:nil];
[[nib objectAtIndex:0] setFrame:self.bounds];
self = [nib objectAtIndex:0];
}
return self;
}
没有得到你..你想要什么? – 2013-02-19 09:57:21
我想加载CustomView和它的xib,但不使用initWithFrame,只需将视图拖到另一个Xib,然后指定class.But不加载XIB。 – 2013-02-19 10:01:50
我想这是你想要做的,不是吗? http://stackoverflow.com/a/5096476/840428 – followben 2013-02-19 10:12:50