如何使先前的标签消失并被释放?

问题描述

| 如何使先前的“标签框”消失并被释放? 我释放对象,但是当我点击并移动时,它仍然只是在“标签框”顶部创建新的“标签框”。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];

    CGPoint nowPoint = [[touches anyObject] locationInView:self.view];
    CGPoint prevPoint = [[touches anyObject] previousLocationInView:self.view];

    CGRect RectFrame1;
    RectFrame1 = CGRectMake(nowPoint.x,nowPoint.y,280,30);
    UILabel *label = [[UILabel alloc] initWithFrame:RectFrame1];
    label.text = [NSString stringWithFormat:@\"x %f   y %f\",nowPoint.x,nowPoint.y];
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor whiteColor];
    [self.view addSubview:label];
    [label release];
    //[self release];
    //[&RectFrame1 release];
}
    

解决方法

如果要从视图中删除它,请使用
removeFromSuperview
[urlabel removeFromSuperview];
    ,你到底打算做什么。是否要删除标签。
[label release]
将从内存中释放实例,而不是从视图中释放实例。
[label removeFromSuperview]
将从视图中删除。但是将其添加到Superview中然后立即将其删除似乎很奇怪。     ,
[self removeChild:label cleanup:YES];  
    ,我认为您正在标签上显示每个接触点。您可以通过修改代码来解决此问题...
CGRect RectFrame1;
UILabel * label = (UILabel *)[self.view viewWithTag:111];
//Here 111 is used you can use your own tags
if(label==nil){   

    RectFrame1 = CGRectMake(nowPoint.x,nowPoint.y,280,30);
    label = [[UILabel alloc] initWithFrame:RectFrame1];    
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor whiteColor];
    label.tag = 111;//Adding tag to our label so that we can call it later.
    label.text = [NSString stringWithFormat:@\"x %f   y %f\",nowPoint.x,nowPoint.y];
    [self.view addSubview:label];
    [label release];

} else {

    RectFrame1 = label.frame;
    RectFrame1.origin = CGPointMake(nowPoint.x,nowPoint.y);
    label.frame = RectFrame1;
    label.text = [NSString stringWithFormat:@\"x %f   y %f\",nowPoint.y];

}
希望这对您有帮助.... :)     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...