1

I'm trying to add a subview with this code:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"subView" owner:self options:nil];
SubView *subView = [subviewArray objectAtIndex:0];
[self.view addSubview:subView];

But only a white rectangle appears, however with the correct size. The SubView's initialization methods aren't called at all. (The initWithCoder:(NSCoder *)aDecoder should be called in this case, but isn't.)

The xib's owner is the SubView class. This is a view and not a controller.

What could cause this malfunction, and what would be the proper solution?

4

1 回答 1

1

使用以下代码从view创建xib

UINib *menuNib = [UINib nibWithNibName:@"MenuView" bundle:nil];
NSArray *aNib = [menuNib instantiateWithOwner:self options:nil];
UIView *menuView = [aNib objectAtIndex:0];
于 2015-05-05T13:19:28.900 回答