A beginner’s question: I am trying to remove a subview after having added it to a view and then releasing it, i.e. I have:
for (int i = 0; i < 9, i++) {
UIButton *btn = [indexButtons objectAtIndex:i];
btn.tag = x;
[notePage1 addSubview:btn];
[btn release];
}
How can I get rid of one of these btn, e.g. number 0? I thought of
UIButton *btn = [indexButtons objectAtIndex:0];
if ([btn isDescendantOfView:notePage1]) { [btn removeFromSuperview]; }
[btn release];
But this will simply crash the app. I get no error log at all? – app simply terminates. How do I do this properly?
You shouldn’t release the button in any of these snippets. You only use
releaseif you have specifically usedretain,alloc,copyornew.Your code should be:
and
Adding to a to a superview increases the retain count automatically and removing releases automatically. You don’t have to worry about any of it.