开发者

How to make the previous label disappear and be released?

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


If you want to remove it from the view use removeFromSuperview

[urlabel 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.


[self removeChild:label cleanup:YES];  


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 .... :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜