I'm trying to create a custom UIView which holds references to its own IBOutlets. I then want to put this custom UIView into another nib.
I'm doing some additional logic in the custom UIView's awakeFromNib
method. Unfortunately, when I try to access the IBOutlets
in awakeFromNib
, they are nil.
Here's the setup:
- I have a
UIView
subclass,CustomView
. - I have a custom
.xib
file with three subviews - In the other nib (that belongs to the view controller), I have dragged a
UIView
onto the view, and then changed the custom class toCustomView
. - I tried setting the view in the
CustomView
nib in IB to a custom classCustomView
and connecting theIBOutlets
to the view, but they were still nil. - I tried setting file owner to
CustomView
and connecting theIBOutlets
to file's owner, but they were still nil. - I also tried using another
IBOutlet UIView *view
and then adding that as a subview toself
inawakeFromNib
but that also didn't do anything.
Here's the code:
// CustomView.h
@interface CustomView : UIView
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UIView *subview1;
@property (nonatomic, retain) IBOutlet UIView *subview2;
// CustomView.m
@implementation CustomView
@synthesize textField, subview1, subview2;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setup];
}
- (void)setup {
// Fails because self.textField is nil
self.textField.text = @"foo";
}