2010-11-30 78 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

您可以通过设置隐藏属性为隐藏意见他们是真实的。

相关问题