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

发布于 2024-11-09 17:02:25 字数 804 浏览 7 评论 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];
}

How to make the previous "label box" disappear and be released?
I release the object but it still just creates new "label boxes" on top of "label boxes" when I tap and move.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼趣 2024-11-16 17:02:25

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

[urlabel removeFromSuperview];

If you want to remove it from the view use removeFromSuperview

[urlabel removeFromSuperview];
如若梦似彩虹 2024-11-16 17:02:25

你到底想做什么。您想删除标签吗?
[label release] 将从内存中而不是从视图中释放实例。
[label removeFromSuperview] 将从视图中删除。但将其添加到超级视图然后立即将其删除似乎很奇怪。

What you exactly mean to do. Do you want to remove the label.
[label release] will release the instance from memory not from the view.
[label removeFromSuperview] will remove from view. But adding it to superview and then removing it immediately seems odd.

紫瑟鸿黎 2024-11-16 17:02:25
[self removeChild:label cleanup:YES];  
[self removeChild:label cleanup:YES];  
这个俗人 2024-11-16 17:02:25

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

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

}

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

I think you are displaying each touched point on a label. You can solve the issue by modifying your code...

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

}

Hope this helps you .... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文