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

タッチした各ポイントをラベルに表示していると思います。コードを変更することで問題を解決できます...

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 に答える
0

あなたが正確に何をしようとしているのか。ラベルを削除しますか。 [label release]ビューからではなくメモリからインスタンスを解放します。 [label removeFromSuperview]ビューから削除されます。しかし、それをスーパービューに追加してからすぐに削除するのは奇妙に思えます。

于 2011-05-25T04:43:42.650 に答える
0
[self removeChild:label cleanup:YES];  
于 2011-05-25T04:46:19.377 に答える