In my application when the user touches the screen, I put an UIImageView on the screen like this:
- (void) drawPoint:(CGPoint) toLocation {
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(toLocation.x, toLocation.y, SIZE_X, SIZE_Y)];
image.tag = 1;
[image setImage:[UIImage imageNamed:@"point.png"]];
[self.myView addSubview:image];
[image release];
}
Here MyView is an UIView.
When the user has finished touching, I want to remove my UIImageView. I have tried this:
- (void) removeFromPoint:(CGPoint) location{
UIImageView *image;
[[image viewWithTag:1] removeFromSuperview];
}
or
- (void) removeFromPoint:(CGPoint) location{
UIImageView *image = (UIImageView *)[self.MyView viewWithTag:1];
[image removeFromSuperview];
}
but both of them end up in EXC_BAD_ACCESS.
Do you know how can I accomplish this?
Thanks.
If you want to remove uimage under the touch try this:
(note I’ve typed it here, i haven’t test that code)
If you wish to debug your code enable NSZombies and check why you gets the bad_access. (check this to learn more about NSZombieEnabled http://www.cocoadev.com/index.pl?NSZombieEnabled)
If you are going to add only one view. Then I would store it in a retained property and I would add/remove it on events like this: