2013-03-05 1 views
0

바 버튼을 두 드릴 때 두 개의 하위보기를 추가하려고합니다. 하지만 하위보기를 추가하면 정상적으로 작동하지만 하위보기를 제거하려고하면 작동하지 않습니다. 여기 바 버튼을 두 드릴 때 하위보기 제거

내가

-(IBAction)showPopover:(id)sender{ 

    UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 

    UIView *popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1000)]; 
    popoverView.alpha = 0.0; 
    popoverView.layer.cornerRadius = 2; 
    popoverView.layer.borderWidth = 0.1f; 
    popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor; 
    popoverView.layer.masksToBounds = YES; 

    popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor; 
    if (popoverCount == 0) { 
     [self.view addSubview:popoverViewBackground]; 
     [self.view addSubview:popoverView]; 
     popoverCount = 1; 
    }else if (popoverCount ==1){ 
     [popoverView removeFromSuperview]; 
     [popoverViewBackground removeFromSuperview]; 
     popoverCount = 0; 
    } 
    [popoverViewBackground setAlpha:0.5]; 
    [popoverView setAlpha:1.0];  
} 

답변

3

문제를 구현하고있는 코드입니다 것은 당신이 당신이 그렇게 된 뷰가 생 같은 코드를 넣어 제거하지 버튼을 클릭 할 때마다 새로운 뷰를 작성하는 것입니다, 그것은 괜찮아요 작동합니다 그것을 시험했다. 하는 .m 파일에 .H 파일에

@interface secondViewController : UIViewController 
{ 

    int popoverCount; 

    UIView *popoverView ; 

    UIView *popoverViewBackground; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 320, 100)]; 

    popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 320, 100)]; 
    popoverView.alpha = 0.0; 
    popoverView.layer.cornerRadius = 2; 
    popoverView.layer.borderWidth = 0.1f; 
    popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor; 
    popoverView.layer.masksToBounds = YES; 

    popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor; 
} 

-(IBAction)showPopover:(id)sender { 

       if (popoverCount == 0) { 
     [self.view addSubview:popoverViewBackground]; 

     [self.view addSubview:popoverView]; 

       [UIView animateWithDuration:0.5 
           animations:^{ 
            popoverView.frame = CGRectMake(0,0,320,100); 
           } 
           completion:^(BOOL finished){ 
            ; 
           }]; 
       [UIView animateWithDuration:0.5 
           animations:^{ 
            popoverViewBackground.frame = CGRectMake(0,0,320,100); 
           } 
           completion:^(BOOL finished){ 
            ; 
           }]; 
     popoverCount = 1; 
    }else if (popoverCount ==1){ 


     [UIView animateWithDuration:0.5 
         animations:^{ 
          popoverView.frame = CGRectMake(0,-100,320,100); 
         } 
         completion:^(BOOL finished){ 
           [popoverView removeFromSuperview]; 
         }]; 
     [UIView animateWithDuration:0.5 
         animations:^{ 
          popoverViewBackground.frame = CGRectMake(0,-100,320,100); 
         } 
         completion:^(BOOL finished){ 
          [popoverViewBackground removeFromSuperview]; 
         }]; 


     popoverCount = 0; 
    } 
    [popoverViewBackground setAlpha:0.5]; 
    [popoverView setAlpha:1.0]; 

} 
+0

고맙습니다. Idon't가 그것을 생각하는 이유를 나는 모른다. – Jonathan

+1

네, 할게요. stackoverflow가 받아 들일 수있게하자마자 P – Jonathan

+0

마지막으로 한 가지만 신경 쓸 수 있을까요? rollover를 첨부하고 popoverview에 애니메이션을 롤업하는 방법이 있습니까? 구글 플러스가 앱에서하는 일처럼? – Jonathan

0
UIView *popoverView; 
UIView *popoverViewBackground; 

.H 파일에 popoverView 및 popoverViewBackground을 선언하고 할당하고 popoverCount가 제로의 경우 이러한 서브 뷰의 초기화한다. 코드에서

-(IBAction)showPopover:(id)sender{ 


    if (popoverCount == 0) { 
    popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 

    popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1000)]; 
    popoverView.alpha = 0.0; 
    popoverView.layer.cornerRadius = 2; 
    popoverView.layer.borderWidth = 0.1f; 
    popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor; 
    popoverView.layer.masksToBounds = YES; 

    popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor; 
     [self.view addSubview:popoverViewBackground]; 
     [self.view addSubview:popoverView]; 
     popoverCount = 1; 
    }else if (popoverCount ==1){ 
     [popoverView removeFromSuperview]; 
     [popoverViewBackground removeFromSuperview]; 
     popoverCount = 0; 
    } 
    [popoverViewBackground setAlpha:0.5]; 
    [popoverView setAlpha:1.0];  
} 
+0

안녕하세요,이 코드를 사용하고 있지만 "속성 'cornerRadius'가 forward class 객체 'CALayer *'에서 찾을 수 없습니다."이 문제에 대한 해결책은 – Dipendra

+0

입니다. @Dipendra : QuartzCore 프레임 작업을 사용하고 작성하십시오. #include .h 파일의이 줄. –

0

적어도 2 문제 :

  1. 방법이 관련 될 때마다, 당신은 당신이 새로 생성 된 인스턴스를 제거하는이 하위 뷰
  2. 의 새로운 인스턴스를 만들 수 있습니다. 즉, 추가 된 이전 인스턴스가 여전히 존재합니다.

당신이 그 (것)를 참조하여 올바르게 제거 할 수 있도록 당신은

  • 는, 인스턴스 변수에서 이러한 하위 뷰를 저장해야합니다.
  • 첫 번째 if-block 내부에서 생성 코드를 이동하면 너무 많은 하위 뷰가 생성되지 않습니다.
0

다른 개체에 addSubview 및 removeFromSuperview를 적용 중이므로 문제가 발생합니다.

첫 번째 showPopover를 호출 할 때 이름이 popoverView이고 popoverViewBackground 인 UIView
의 두 객체를 만듭니다.
그리고 이것을 self.view에 추가하십시오.

지금까지 모든 것은 잘하지만,이 메서드 호출 두 번째 popoverView 및 popoverViewBackground의
새로운 개체가 다시 생성
당신이없는 self.view에서 그 새로운 생성 된 객체를 제거하려고 할 때.

이 문제를 해결하는 방법 :이 문제를 해결하는 두 가지 방법이 있습니다

: 당신은 어디서든 액세스 할 수 있도록

1) .H 파일에이 객체를 생성합니다.

2) 메서드를 처음 호출 할 때만 생성되는 방식으로 개체를 만듭니다. UIView 태그에서이 태그를 찾아서 제거하십시오.서브 뷰

웨이 1은 쉬운 코드이지만 약간의 코드가 필요하지만 메모리를 절약 할 수 있습니다.

모두 최고

관련 문제