Apologies because I see people have asked questions like this before. However, I haven't had much luck following the instructions. I'm interested in creating a custom view with a xib file and reusing it in another view controller's xib file.
Existing Posts:
Using xib object inside another xib
How to use a xib and a UIView subclass together?
I have already:
- Created a custom xib file (let's call it CustomView.xib), and corresponding .h and .m class files
- Set the File's Owner of the xib file to CustomView
- Created a top level UIView with other views as children (UILabel's etc)
- NOTE this has a child UIView which is a custom view written in code as well
- Wired up the IBOutlets
- Created a ViewController.xib file
- Added a UIView, set the class to CustomView and also wired that up
This results in a blank view showing up when I build the application.
I then tried what one of the above posts said to do which was override CustomView's initWithDecoder method and adding the following:
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];
This unfortunately was resulting in the following error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<UIView 0x548ff00> setValue:forUndefinedKey:]:
I thought this might be because I didn't set the top level view in CustomView.xib to be of class CustomView. So I then changed it to CustomView, and then things went into infinite recursion. Which makes sense since it's just reloading itself over and over again.
Not sure what I missed from those previous posts, but I'd appreciate any guidance! Thanks!