2011-05-25 47 views
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]; 
} 
+0

JDL,您一直在圍繞一個星期,問的問題了一把。現在您已經學會了如何在問題中設置代碼的格式,所以我們不必爲您清理它。 http://stackoverflow.com/editing-help – 2011-05-25 04:43:05

+0

我今天早上剛剛開始。但我會嘗試從您的鏈接學習如何格式化代碼。謝謝。 – jdl 2011-05-25 05:08:11

回答

1

如果你想從視圖中使用removeFromSuperview

[urlabel removeFromSuperview]; 
+0

謝謝您......在新實例工作開始之前,按照您指定的地址跟蹤地址並釋放。 – jdl 2011-05-25 04:58:18

0

什麼你恰恰意味着做刪除。你想刪除標籤嗎? [label release]將從內存中釋放實例,而不是從視圖中釋放。 [label removeFromSuperview]將從視圖中刪除。但是將其添加到超級視圖然後立即刪除似乎很奇怪。

0
[self removeChild:label cleanup:YES]; 
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]; 

} 

希望這有助於你.... :)

+0

謝謝......我用一個整型變量做了類似的事情來跟蹤指針的地址。但很高興看到其他方式。 – jdl 2011-05-25 05:22:05