Ios 如果没有addSubView问题,请从SuperView中移除

Ios 如果没有addSubView问题,请从SuperView中移除,ios,addsubview,Ios,Addsubview,当我每秒调用reloadData方法时,我的代码工作正常,它删除视图(当有视图时)并添加子视图。问题是当[self.lbl1 removeFromSuperview]函数上没有显示我得到的子视图exc\u bad\u access问题时 我的代码 -(void)reloadData if (result1 > result2 && al == YES) { lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(320

当我每秒调用
reloadData
方法时,我的代码工作正常,它删除视图(当有视图时)并添加子视图。问题是当
[self.lbl1 removeFromSuperview]
函数上没有显示我得到的子视图
exc\u bad\u access
问题时

我的代码

-(void)reloadData

if (result1 > result2 &&  al == YES)
{
    lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(320, 530, 550, 200)] autorelease];

    lbl1.userInteractionEnabled = NO;
    lbl1.text = @"WARNING";
    lbl1.tag = 30;
    lbl1.font = [UIFont fontWithName:@"Helvetica" size:18.0];
    lbl1.textColor = [UIColor redColor];
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.lineBreakMode = NSLineBreakByWordWrapping;
    lbl1.numberOfLines = 2;
    [self addSubview:lbl1];
}

    else if (result1 < result2 &&  al == YES){

    [self.lbl1 removeFromSuperview];
}
-(void)重新加载数据
如果(result1>result2&&al==YES)
{
lbl1=[[UILabel alloc]initWithFrame:CGRectMake(320530550200)]自动释放];
lbl1.userInteractionEnabled=否;
lbl1.text=@“警告”;
lbl1.tag=30;
lbl1.font=[UIFont fontWithName:@“Helvetica”大小:18.0];
lbl1.textColor=[UIColor redColor];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.lineBreakMode=NSLineBreakByWordWrapping;
lbl1.numberOfLines=2;
[自添加子视图:lbl1];
}
else if(result1

请问我的问题在哪里?

在这种情况下,您可以这样检查

else if (result1 < result2 &&  al == YES)
{
    for(UIView *view in self.view.subviews)
    {
        if ([view isKindOfClass:[UILabel class]])
        {
            [self.lbl1 removeFromSuperview];
        }
    }
}
else if(result1
在第一个if循环之前调用第二个if循环(
result1
)时,代码中是否存在条件

在这种情况下,
lbl1
将不会添加到视图中,也不会被分配,因此无法删除

您需要检查lbl1是否存在,然后将其从superview中删除

if(self.lbl1) [self.lbl1 removeFromSuperView];

尝试删除autorelease并使其类似于

 if (lbl1) {
        [lbl1 removeFromSuperview];
        [lbl1 release];        
    }

lbl1=[[[UILabel alloc]initWithFrame…

之前,您还可以检查标签是否具有正确的超级视图

if (lbl1 && lbl1.superview == self) { // then lbl1 is already a subview of self
    [lbl1 removeFromSuperview];
}

谢谢。但它不起作用。我正在获得EXC\u BAD\u访问权限(code=1,address=0x2e777783)。当我将所有
lbl1
标签方法与
SELF
放在一起时,它就起作用了。谢谢,它现在起作用了。