xcode 从其 Superview 中删除 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4914886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove UIImageView from its Superview
提问by CristiC
In my application when the user touches the screen, I put an UIImageView
on the screen like this:
在我的应用程序中,当用户触摸屏幕时,我会UIImageView
像这样在屏幕上放置一个:
- (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
.
这MyView
是一个UIView
.
When the user has finished touching, I want to remove my UIImageView
. I have tried this:
当用户完成触摸时,我想删除我的UIImageView
. 我试过这个:
- (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?
但它们都以 EXC_BAD_ACCESS 结束。你知道我怎样才能做到这一点吗?
Thanks.
谢谢。
采纳答案by Piotr Czapla
If you want to remove uimage under the touch try this:
如果你想在触摸下删除 uimage 试试这个:
UIView * imageView = [self.myView hitTest:location withEvent:nil];
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag == 1) {
[imageView removeFromSuperview];
}
(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)
如果你想调试你的代码,启用 NSZombies 并检查为什么你会得到 bad_access。(检查这个以了解更多关于 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:
如果您只想添加一个视图。然后我将它存储在一个保留的属性中,我会在这样的事件中添加/删除它:
@property (nonatomic,retain) UIImageView* marker;
@interface
@syntesize marker;
-(void) onTouchDown {
[self.view addSubview:marker];
}
-(void) onTouchUp {
[marker removeFromSuperview];
}
回答by Alexander Belyavskiy
The first example of removing image view is wrong.
In the second one what is POINT_TAG equal to?
Try to use tag greater then 9.
Try to debug what UIImageView *image = (UIImageView *)[self.MyView viewWithTag:POINT_TAG];
returns.
第一个删除图像视图的例子是错误的。在第二个中 POINT_TAG 等于什么?尝试使用大于 9 的标记。尝试调试UIImageView *image = (UIImageView *)[self.MyView viewWithTag:POINT_TAG];
返回的内容。