First time asking a question here.
I have a question, I have a UIImageView added as a subview to my UIButton, which is declared using buttonWithType: (which means I don’t have to release the button right?) But do I still have to release the subview of my UIButton?
Code bits:
UIImage *circleImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"item-circle" ofType: @"png"]];
UIImageView *circleImageView = [[[UIImageView alloc] initWithImage: circleImage] autorelease];
[imageView setFrame: CGRectMake(-5, -5, 65, 65)];
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button addSubview: circleImageView];
Short Answer:
Your code seems fine. The basic rule of thumb is that for every
alloc,new,retain, orcopy, you need areleaseorautorelease.Long Answer:
Let’s go through your code line by line.
This first line uses a convenience method. you don’t need to call release on anything, since you haven’t called
alloc,new,retain, orcopy.In the second line, you call
alloc, but you then callautoerelease, so you’re good there.Again, no
alloc,new,retain, orcopy.Once again, you’ve used a convenience method.
You still haven’t called
alloc,new,retain, orcopy. Therefore, you don’t callreleaseorautorelease.