This is another \"I\'m confused question\". So I\'m working on bringing in custom views into a view controller. I\'ll just outline the exact steps for the error.
Set the file's owner == your UIView subclass so that you can connect outlets to it. And you should set the class of nib-painted UIView to that same subclass because it's an instance of that UIView subclass.
In other words, follow these steps:
Your crash is happening because your code says this:
UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"theNIB" owner:self options:nil] objectAtIndex:0];
But that owner:self
is the view controller where this code is running. You want the view subclass to be the nib's owner.
To fix, give your UIView subclass the job of init-ing itself from the nib, like this:
CustomView.h
@interface CustomView : UIView
- (id)initFromNib;
@end
CustomView.m
#import "CustomView.h"
@interface CustomView ()
// connect this in the XIB to file's owner that you've set to this CustomView class
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
@implementation CustomView
- (id)initFromNib
{
self = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] objectAtIndex:0];
if (self) {
// prove you can set properties on your outlets
self.myLabel.text = @"this is good";
}
return self;
}
I built a little project with just this stuff in it as described. Works fine. Lemme know if you'd like to see it and I'll find a way to send you an anonymous zip.