I have some custom created UIViews that use .xib files for layout, and backing classes for extra setup. I create these classes using alloc/init and calling loadNibNamed in my custom init method but in doing so was causing memory leak. Someone pointed out that the alloc portion actually created a self object that was leaking so I adjusted my init method to this one:
- (id)init
{
[self autorelease];
self = [[[[NSBundle mainBundle] loadNibNamed:@"AssignmentView" owner:nil options:nil] lastObject] retain];
[self setupBranding];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
[self addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
return self;
}
However, now when I run analyze code I get this warning "Returning 'self' while it is not set to the result of '[(super or self) init...]'". So my question is what is the correct way for doing custom UIViews with a backing class?
Since it was asked I'd used this above code like this:
AssignmentView * assignmentView = [[AssignmentView alloc] init];
[self.view addSubview:assignmentView];
loadNibNamed
in whatever class you're creating it in and then setting it up in itsawakeFromNib
method, not even touchinginit
. This has been my experience, anyway. Could you perhaps add some code where you are instantiating the view, and yourawakeFromNib
method? – Paul O.self = [super init]
at the top of your method. Paul is right though, in that you normally aren't doing the nib loading internally. – Dustin