如何让以前的标签消失并被释放?

时间:2011-05-25 04:39:21

标签: iphone uilabel cgpoint

如何使之前的“标签盒”消失并被释放? 我发布了这个对象,但是当我点击并移动时,它仍然只是在“标签框”的顶部创建了新的“标签框”。

- (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];
}

4 个答案:

答案 0 :(得分:1)

如果要将其从视图中删除,请使用removeFromSuperview

[urlabel removeFromSuperview];

答案 1 :(得分:0)

你究竟要做什么。要删除标签吗? [label release]将从内存中释放实例,而不是从视图中释放实例。 [label removeFromSuperview]将从视图中删除。但是将它添加到superview然后立即删除它似乎很奇怪。

答案 2 :(得分:0)

[self removeChild:label cleanup:YES];  

答案 3 :(得分:0)

我认为您正在标签上显示每个触摸点。您可以通过修改代码来解决问题......

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.x, nowPoint.y];

}

希望这可以帮助你.... :)