2011-04-10 5 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) (viewarray에있는 UIView *보기)에 대한 { \t \t \t \t \t \t { \t \t \t \t [view removeFromSuperview]; \t \t \t} \t \t \t \t \t} –

+1

시스템 자체가에 추가 된 모든 뷰에 대한 조항이 있기 때문에이 viewarray을 사용하는 이유는 단지 다른과 전망 태그를 추가 할 필요가 그래서 나는 ..... 모른다 값 ........ 이것은 메모리를 절약하는 데 도움이됩니다. 감사 – Ravin

0

user698952의 답변은 괜찮습니다. 태그를 추가하는 것이 좋습니다.

myAddedView.tag = 10; 

동일한 태그가있는 뷰가 두 개 이상인 경우 제거 코드를 약간 변경해야합니다.

UIView *someView = nil; 
while (someView = [self.view viewWithTag:10]) { 
    [someView removeFromSuperview]; 
} 
관련 문제