2014-10-23 3 views
0

단추 작동에 문제가 있습니다 ... 왜 작동하지 않는지 이해할 수 없으며 오류를 이해하는 데 도움을 줄 수 있는지 물어보고 싶습니다.단추 작업으로 UiView removeFromSuperview 제거

내 viewController에서 선택한 모드에서 단추가 다른 UIView 안에 UIView를 추가하지만 단추를 가져올 때 이전에 입력 한보기를 제거해야합니다 가져온 경우, 아무 것도하지 않고 거기에 남아 있습니다. 여기

- (IBAction)shareActive:(id)sender { 
     UIView *checkActive = [[UIView alloc]init]; 
     checkActive.frame =CGRectMake(2, 2, 11, 11); 
     checkActive.layer.masksToBounds = YES; 
     checkActive.layer.cornerRadius = 5.5f; 
     checkActive.backgroundColor = [UIColor greenColor]; 


     if (!self.condividiButton.selected) { 

       self.condividiButton.selected = YES; 
      [self.checkCondividi addSubview:checkActive]; 

       NSLog(@"attivo"); 

     }else { 
      self.condividiButton.selected = NO; 
        [checkActive removeFromSuperview]; 

      NSLog(@"disattivo"); 


     } 
    } 
+0

이 메소드가 호출 될 때 항상 당신은'checkActive'와 같이 새로운 UI 인스턴스 _ UIView를 생성했습니다. 분명히 그 인스턴스는 이전 세션 동안 superview에 추가 한 것과 완전히 다른 인스턴스입니다. 따라서 제거하려는 경우 superview에 추가 된 적이없는 _ 새 인스턴스로 작업하고 있습니다. 어떤 영향을 미치지. – holex

+0

사실 ... 오류를 발견했습니다 ... 헤더 파일에 속성을 만들고 viewDidLoad에서 뷰를 초기화 한 다음 뷰의 단추 메서드에서 뷰에서 추가 및 제거를 지정한 다음 작품 .. 도움을 주셔서 대단히 감사합니다 ... :) – kAiN

답변

1

가 많은 가능한 솔루션 하나입니다 틀렸다 경우이 방법은,하지만 난 이해가 안 돼요.

다른 사람들도 이러한 문제를 해결할 수있는 방법에 대한 아이디어를 줄 것이라고 확신합니다.

- (IBAction)buttonShareTouchedUpInside:(UIButton *)sender { 
    if (!self.condividiButton.selected) { 
     UIView *checkActive = [[UIView alloc]init]; 
     checkActive.tag = 121212; 
     checkActive.frame =CGRectMake(2, 2, 11, 11); 
     checkActive.layer.masksToBounds = YES; 
     checkActive.layer.cornerRadius = 5.5f; 
     checkActive.backgroundColor = [UIColor greenColor]; 

     self.condividiButton.selected = YES; 
     [self.checkCondividi addSubview:checkActive]; 
    } else { 
     self.condividiButton.selected = NO; 
     [self.checkCondividi.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) { 
      if (obj.tag == 121212) { 
       [obj removeFromSuperview]; 
      } 
     }]; 
    } 
} 

참고 : 원래 문제가 많은 다른 아마 더 우아한 방법을 통해 해결할 수 있습니다,하지만 난 여기에 대해 관련된 아니에요.