Search code examples
iosuiviewstoryboardloadnibnamed

Storyboard add a custom view which has xib file has no subviews


I create a custom UIView A with a xib file and it can be loaded correctly by:


    NSArray *starsNib = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:nil options:nil];
    A *starsView = starsNib[0];
    [self.view addSubview:starsView];

Then in my storyboard file, I add this custom view A to the UITableViewCell's contentView. When the tableview is loading, I find the custom view A has no subviews which are added in the xib file. I also find when the A's initWithCoder: returns, it has no child views.
BTW, in the xib file, I have set both the parent view and file's owner to the custom view A class.
I'd like to know why this happens and why the subviews are not loaded when initWithCoder: returns?


Solution

  • Because the view is being loaded from the storyboard, not the XIB. And in the storyboard the view is empty.

    You should remove the view from the storyboard and load it in code, probably when the cell is instantiated as it only needs to be done once per cell.