2011-04-10 82 views
0

如何從超級視圖中刪除子視圖並點擊按鈕點擊重繪?如何從超級視圖中刪除子視圖並單擊按鈕單擊重新繪製?

float padding = 5.0; 
     float view_width = 95.0; 
     float view_height = 120.0; 
     int rows = 0.0f; 
     int columns = 0.0f; 



     UIView *myAddedView ; 


     for (int i=0; i<[product.CorrentAnswer intValue]; i++) 
     { 
      if(i%3 == 0 && i > 0) 
      { 
       columns = 0.0f; 
       rows += view_height; 
      } 

      myAddedView =[[[UIView alloc] initWithFrame:CGRectMake(padding+columns, rows, view_width, view_height)] autorelease]; 
      myAddedView.backgroundColor = [UIColor clearColor]; 


      CGRect myImageRect = CGRectMake(40, 100.0f, 40.0f, 40.0f); 
      UIImageView *myImage = [[UIImageView alloc]initWithFrame:myImageRect]; 

      NSString *imageName = [NSString stringWithFormat:@"%@",product.imagename]; 
      [myImage setImage:[UIImage imageNamed:imageName]]; 
      [myAddedView addSubview:myImage]; 
      [viewarray addObject:myAddedView]; 

      [self.view addSubview:myAddedView]; 
      columns+= view_width; 
     } 

我想從self.view中刪除myAddedView?

希望能得到很快回答....

回答

2

可以通過指定一個標籤這樣做是爲了myAddedView ....

代碼更改:

myAddedView =[[[UIView alloc] initWithFrame:CGRectMake(padding+columns, rows, view_width, view_height)] autorelease]; 
      myAddedView.backgroundColor = [UIColor clearColor]; 
//add this line 
myAddedView.tag = 10; 

和按鈕點擊(當你想刪除子視圖時)寫:

if([self.view viewWithTag:10]!=nil) 
{ 
    [[self.view viewWithTag:10] removeFromSuperView]; 
} 

謝謝

+1

以及感謝這樣一個快速回復隊友,但我只是用下面的代碼的幫助,這樣做,如果(viewarray.count> 0){ \t \t \t爲(UIView的*在viewarray視圖) \t \t \t { \t \t \t \t [view removeFromSuperview]; \t \t \t} \t \t \t \t \t} – 2011-04-10 15:06:35

+1

我不知道爲什麼你使用這個viewarray因爲系統本身具有規定上添加了所有的意見.....所以你只需要來標記不同的看法值........這將幫助您節省內存。謝謝 – Ravin 2011-04-10 15:13:35

0

user698952的答案沒問題,添加標籤是最好的選擇。

myAddedView.tag = 10; 

如果您有多個具有相同標記的視圖,您必須更改一小段刪除代碼。

UIView *someView = nil; 
while (someView = [self.view viewWithTag:10]) { 
    [someView removeFromSuperview]; 
} 
相關問題