Load custom UIView with XIB from a View Controller's view using IB
You are correct. IB uses initWithCoder. initWithCoder should look very similar to your other init methods:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// CUSTOM INITIALIZATION HERE
}
return self;
}
Once you assign your class within IB, you won't need to instantiate it from the bundle unless I'm misunderstanding your intention.
Daniel
Updated on June 17, 2022Comments
-
Daniel almost 2 years
I have a custom
UIView
(MyCustomUIView
) which is built using Interface Builder. I'd like to place this custom view inMyViewController
's view, which is also designed using IB. I've placed anUIView
as a subview inMyViewController
's XIB and set it's class toMyCustomUIView
. The problem is, when I run the code, only a blank view appears. (When I instantiateMyCustomUIView
in code, it displays well.)I'm only overriding the
initWithFrame:
method the following way inMyCustomUIView.m
:- (id)initWithFrame:(CGRect)frame { [[NSBundle mainBundle] loadNibNamed:@"MyCustomUIView" owner:self options:nil]; self = self.view; return self; }
What should I do to make the view load properly? How should
initWithCoder:
look like?