0

如何让之前的“标签框”消失并被释放?我释放了该对象,但当我点击并移动时,它仍然只是在“标签框”之上创建新的“标签框”。

- (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

4 回答 4

1

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

[urlabel removeFromSuperview];
于 2011-05-25T04:42:51.290 回答
0

你到底是什么意思。是否要删除标签。 [label release]将从内存而不是视图中释放实例。 [label removeFromSuperview]将从视图中删除。但是将其添加到 superview 然后立即将其删除似乎很奇怪。

于 2011-05-25T04:43:42.650 回答
0
[self removeChild:label cleanup:YES];  
于 2011-05-25T04:46:19.377 回答
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];

}

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

于 2011-05-25T05:03:31.863 回答