I'm trying to create a custom UIView
that brings in it's view from a nib file.
In my controller I have something like:
self.arcView=[[JtView alloc] initWithCoder:self];
self.arcView.backgroundColor=[UIColor redColor];
self.arcView.frame=CGRectMake(30.0f,200.0f, 100.0f, 100.0f);
[self.view addSubview:self.arcView];
My first question is what should go into the argument for initWithCoder (NSCoder *)
? I tried self but got an incompatible pointer type but this seemed to work. But on to question #2:
Second, the argument is that you use initWithCoder
with nibs
and initWithFrame
when putting your custom view in a frame. Well, I want to load a nib
in my custom view and then put it into a frame. Can I just add a frame as above and it's ok (it looks like it works)?
You are doing it the other way around: it's not you who should call initWithCoder:
, it's the implementation of the loadNibNamed:owner:
method that does it.
What you need to do in your code is calling
UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"theNIB"
owner:self
options:nil] objectAtIndex:0];
This would unbundle the NIB, and call your initWithCoder:
initializer, and give you back a view with all the outlets connected.