簡體   English   中英

隱藏其他視圖控制器的子視圖

[英]Hide subview of different view controller

我有一個視圖用作另一個自定義警報視圖的背景(淺灰色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];

如何始終關閉背景視圖?

謝謝

您似乎並沒有刪除視圖,而是通過將alpha設置為零來使其不可見。 因此,每次調用第二個代碼示例時,都會在self.view添加新版本的背景視圖和對話框視圖。 在第二個調用中,您將有兩個背景視圖,它們的標簽均為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中釋放控制器ivars。

您可以通過將HIDE屬性設置為true來隱藏它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM