removeFromSuperview不起作用 [英] removeFromSuperview not working

查看:342
本文介绍了removeFromSuperview不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图中我添加了imageView但是在我删除它之后这个图像保持在视图上,如何删除它?

To my view I add imageView but after I remove it this image stay on view, how remove it ?

创建:

(void)handleTapHold:(UILongPressGestureRecognizer *)gestureRecognizer{
UIImageView *pinkIm = [[UIImageView alloc]initWithFrame:CGRectMake(self.roundView.frame.origin.x + 70, self.roundView.frame.origin.y - 70, 60, 60)];
[pinkIm setImage:[UIImage imageNamed:@"pink_print"]];
pinkIm.layer.cornerRadius = pinkIm.frame.size.height / 2;
pinkIm.layer.borderWidth = 3.0f;
pinkIm.layer.borderColor = [UIColor whiteColor].CGColor;
pinkIm.clipsToBounds = YES;

[self.view addSubview:pinkIm];

 if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
  [pinkIm removeFromSuperview];
 }
}

稍后用同样的方法我尝试删除但没有任何反应:

later in same method I try to Remove but nothing happen:

 [pinkIm removeFromSuperview];

发现我的问题,在开始时我不添加:

Find my problem, in start I don't add:

if (gestureRecognizer.state == UIGestureRecognizerStateBegan){


推荐答案

当你尝试删除它时,* pinkIm已经是零 - 你可以发布剩下的代码,以便我们可以确保指针pinkIm是不是没有?

It might be that by the time you try to remove it, *pinkIm is already nil - could you post the rest of the code so that we can make sure the pointer pinkIm is not nil?

如果它是零,并且你无能为力,另一种删除它的方法是通过它找到它当前观点的孩子们。示例:

In the case that it is nil, and there is nothing you can do about it, the other way to remove it would be to find it through the current view's children. Example:

for(UIView *child in self.children){
if([child isKindOfClass: [UIImageView class]]){
[child removeFromSuperView];
break;}}

注意:如果您有多个图像视图,则上述操作无效。在这种情况下,您可能希望继续检查child的状态,以确保您正在讨论* pinkIm指向的内容。

Note: The above will not work if you have several image views. In that case, you may want to keep checking the state of "child" to ensure that you are talking about what *pinkIm was pointing to.

请发布方法的其余代码!

Please post rest of code of the method!

这篇关于removeFromSuperview不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆