Reputation: 16051
I have a problem with my app where the code for which is far too long to go into, but suffice to say when i'm removing a UIView and replacing it with a new one like so:
NSLog(@" .. %@", (Icon *)[self viewWithTag:index]);
Icon *icon = (Icon *)[self viewWithTag:index];
CGRect frame = icon.frame;
int tag = icon.tag;
[icon removeFromSuperview];
[icon release];
Icon *icon2 = [[Icon alloc] init];
icon2.frame = frame;
[icon2 makeIconStandardWithTag:(int)tag];
[self addSubview:icon2];
It does some weird thing where that NSLog the first time (because the view is already there) shows that the object is an icon, but the second time after running this code shows that it's a UIImageView for some reason now, and it displays what i presume to be the original icon at some odd position on the screen. It's very erratic behaviour. But what i do know is this:
Removing the [icon removeFromSuperview]; line, although keeping the object there, stops this behaviour and causes the NSLog to return an Icon, as it should.
So my guess is that it's not removing icon correctly. Is there a way to completely remove icon, or is removeFromSuperview as far as i can go. What i could do is just have it set to alpha = 0 but this is more of a patch-over solution and not how i want to solve it.
Upvotes: 0
Views: 85
Reputation: 17898
Did you retain icon somewhere prior to this? If not, no need to release it after the call to removeFromSuperview. Similarly, unless you need the reference to icon2 elsewhere, you can release that after calling addSubview.
Views retain views added via addSubview, and they release views removed via removeFromSuperview.
Upvotes: 0
Reputation: 7417
This is a guess, but try setting self.tag
to -1 or some other value that doesn't collide with the tags you're setting on your Icon
objects. The viewWithTag:
method searches the current view and its subviews for a match, so if self.tag == 0
and you call [self viewWithTag:0]
, you'll get self
.
Upvotes: 0
Reputation: 12106
Can you verify what "self" is in this line of code: It might not be what you think.
[self addSubview:icon2];
NSLog(@" Self is %@", self);
Upvotes: 1
Reputation: 22266
"Is there a way to completely remove icon, or is removeFromSuperview as far as i can go"
You can set the object to nil:
icon = nil;
Upvotes: 1