edit #1
please read the edit #1 at the bottom of the question; I'm trying to figure out all the ways nib's can be used in creating custom views so all the examples deal with nib files.
end
I have been trying to figure out all the techniques for creating custom views that use nibs in controllers. I have listed the ways at the bottom of this question.
Here's the code for my TRtestview (one implication of how I have this set-up is that initWithCoder will always be called):
// TRtestview.m
-(id)initWithCoder:(NSCoder *)aDecoder{
self=[super initWithCoder:aDecoder];
if (self) {
if (self.subviews.count == 0) {
UIView *myView=[[[NSBundle mainBundle] loadNibNamed:@"testview" owner:nil options:nil] lastObject];
myView.userInteractionEnabled=YES; // doesn't do anything
[self addSubview:myView];
}
}
NSLog(@"about to return here in initWithCoder");
return self;
}
edit 1
-(void)setup
{
UIView *myView=[[[NSBundle mainBundle] loadNibNamed:@"testview" owner:nil options:nil] lastObject];
[self addSubview:myView];
}
- (id)initWithFrame:(CGRect)frame
{
NSLog(@"at top of initWithFrame");
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
in TRViewController.m, I tried setting thisView to userInteractionEnabled but it didn't change things:
- (void)viewDidLoad
{
[super viewDidLoad];
self.thisView.userInteractionEnabled=YES;
This has worked for the scenario of calling initWithFrame (technique #1) and loadNibNamed (technique #2). It will load the view by dragging it out from the Object Inspector and then setting the Class in the Custom Class attribute inspector (technique #3). However, the button is non-functional and thus this way doesn't work.
How would I create a custom view with a nib using technique #3 and allow for user interaction?
thx
edit 1
I'd really like to understand all the ways that a custom view can be created and instantiated. Ideally, I'd like to be able to have custom views that can be created via initWithFrame, loadNibNamed, or dragged from the object library.
It seems like the scenarios are:
- initWithFrame from a ViewController; if using with a nib, I call loadNibNamed in my initWithFrame - I need to prevent recursive loading
- loadNibNamed from a ViewController (which will call initWithCoder) and would want to load only once
- dragging an instance of a UIView and then setting the custom class to my custom UIView (TRtestview in this case). This will call initWithCoder.