问题描述
|
如何使先前的“标签框”消失并被释放?
我释放对象,但是当我点击并移动时,它仍然只是在“标签框”顶部创建新的“标签框”。
- (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];
}
希望这对您有帮助.... :)