2013-03-05 107 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

問題的代碼是,你每次創建新的視圖,你單擊該按鈕,以便舊觀點不刪除把你的代碼像THI,那麼它會工作fine.I已經測試過它。

在h文件

@interface secondViewController : UIViewController 
{ 

    int popoverCount; 

    UIView *popoverView ; 

    UIView *popoverViewBackground; 
} 

在.m文件

- (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

非常感謝。我不知道爲什麼我沒有想到這一點。 – Jonathan 2013-03-05 06:15:04

+1

是的,我會的。只要stackoverflow讓我接受它:P – Jonathan 2013-03-05 06:16:28

+0

我可以請你打擾最後一件事嗎?有沒有辦法將一個下拉和捲起動畫連接到我的popoverview?比如谷歌加在他們的應用程序呢? – Jonathan 2013-03-05 06:23:30

0
UIView *popoverView; 
UIView *popoverViewBackground; 

聲明popoverView和popoverViewBackground在h文件和分配,並且這些子視圖的在時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'無法找到轉發類對象'CALayer *'」任何解決方案爲此 – Dipendra 2013-03-05 06:55:11

+0

@Dipendra:使用QuartzCore框架工作和寫#include .h文件中的這一行。 – 2013-03-05 06:58:36

0

至少有2個問題在您的代碼:

  1. 每次方式參與,您正在創建的這些子視圖
  2. 要刪除的新創建的實例新實例。也就是說,之前添加的實例仍然存在。

你應該

  • 保存在實例變量,這些子視圖,讓您可以參考他們,正確刪除它們。
  • 將創建代碼移動到第一個if塊中,以便您不會創建太多的子視圖。
0

這是因爲您在不同的對象上應用addSubview和removeFromSuperview而發生的。

當第一次showPopover被調用時,它創建兩個對象UIView
名稱爲popoverView和popoverViewBackground。
並將其添加到self.view。

直到現在每一件事情很好,但是當這個方法調用第二次
popoverView和popoverViewBackground的新對象再次創建和
你正試圖從self.view這是不存在刪除新創建的對象。

如何解決這個問題:

有解決這個雙向的:

1)創建.h文件中這個對象,所以你可以從任何地方訪問它。 2)創建對象的方式只能在第一次調用方法時創建。 給這個UIView標籤,當你移除在UIView中找到這個標籤。子視圖

方法1是簡單的方法2需要一些代碼,但你可以使它在記憶體中有效。

全部最好

相關問題