如何解决如何使先前的标签消失并被释放?
| 如何使先前的“标签框”消失并被释放? 我释放对象,但是当我点击并移动时,它仍然只是在“标签框”顶部创建新的“标签框”。- (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];
}
希望这对您有帮助.... :)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。