2010-11-30 64 views
0

我有一個視圖用作另一個自定義警報視圖的背景(淺灰色0.5 alpha)。隱藏不同視圖控制器的子視圖

當用戶點擊我的確定按鈕上的自定義提醒,我想隱藏自定義提醒和背景視圖也。

這兩種觀點都是一樣的上海華子視圖...

我這樣做的buttonTapped:方法隱藏的意見,它適用於第一次嘗試,但是從第二次起,後臺視圖永遠不會解僱......警報每次都會正確隱藏。

[UIView animateWithDuration:0.5f animations:^{ 
    self.view.alpha=0.0f; //hide alert 
    [self.view.superview viewWithTag:1].alpha=0.0f; //hide background  
}]; 

它們添加爲子視圖,如下所示:

ResultDialogController *dialogController = [[[ResultDialogController alloc] initWithNibName:@"ResultDialogController_" bundle:nil] retain]; 
ResultBackgroundViewController *bgViewController = [[[ResultBackgroundViewController alloc] initWithNibName:@"ResultView" bundle:nil] retain]; 

dialogController.view.alpha=0; 
bgViewController.view.alpha=0; 
bgViewController.view.tag=1; 

[UIView animateWithDuration:0.5f animations:^{ 
    bgViewController.view.alpha=0.5f;          
    dialogController.view.alpha=1.0f; 
    }]; 

[self.view addSubview:bgViewController.view]; 
[self.view addSubview:dialogController.view]; 
[dialogController release]; 
[bgViewController release]; 

我怎麼能總是解僱的背景有何看法?

感謝

回答

1

您似乎沒有刪除視圖,只是通過將alpha設置爲零來使其不可見。所以每次你打電話給你的第二個代碼示例時,你都會添加一個新版本的背景視圖和對話視圖到self.view。在第二次調用中,您將獲得兩個背景視圖,都是tag = 1,並且您將從調用[self.view.superview viewWithTag:1]獲得第一個背景視圖,這就是您新添加的背景視圖不會隱藏的原因。

但是,這還不是全部,你也有一個內存泄漏ResultDialogControllerResultBackgroundViewController。當您致電initWithNibName:bundle:時,撥打retain不是必需的。也許你這樣做是因爲當你釋放控制器時你有些崩潰了?

你應該做的是爲你的控制器創建ivars和屬性。

@property (nonatomic, retain) ResultDialogController *resultController; 
@property (nonatomic, retain) ResultBackgroundController *backgroundController; 

然後顯示控制器時,你可以這樣做:

ResultDialogController *dialogController = [[ResultDialogController alloc] initWithNibName:@"ResultDialogController_" bundle:nil]; 
self.dialogController = dialogController; 

ResultBackgroundViewController *bgViewController = [[ResultBackgroundViewController alloc] initWithNibName:@"ResultView" bundle:nil]; 
self.backgroundController = bgViewController; 

// do the same as before 

然後在buttonTapped:你做:

[UIView animateWithDuration:0.5f 
    animations: ^{ 
     self.dialogController.view.alpha = 0; 
     self.backgroundController.view.alpha = 0; 
    } 
    completion: ^(BOOL finished){ 
     [self.dialogController.view removeFromSuperview]; 
     [self.backgroundController.view removeFromSuperview]; 
    } 
]; 

而且最糟糕的是,不要忘記釋放控制器處於dealloc狀態。

+0

非常感謝,幾個編輯雖然...在你的`animateWithDuration:0.5f`之後,你有'持續時間'一詞不應該在那裏。此外,完成塊處理程序應該是`完成:^(BOOL完成){` – joec 2010-11-30 19:45:51

0

您可以通過設置隱藏屬性爲隱藏意見他們是真實的。

相關問題