开发者

Hide subview of different view controller

I have a view that acts as a background (light grey 0.5 alpha) for for another custom alert view.

When the user taps my OK button on the custom alert, i want to hide the custom alert and the background view also.

Both views are subviews of the same superview...

I do this in the buttonTapped: method to hide the views, and it works for the first attempt, but from the second time onwards, the background views never dismiss... the alerts hide correctly every time.

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

They are added as subviews, as follows:

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];

How can i always dismiss the backg开发者_运维百科round view?

Thanks


You don't seem to remove the views, you are just making the invisible by setting the alpha to zero. So every time you call your second code sample you will add a new version of the background view and the dialog view to self.view. At the second call you will have two background views, both with tag = 1 and you are getting your first background view from the call to [self.view.superview viewWithTag:1] which is why your newly added background view does not get invisible.

But that is not all, you also have a memory leak for ResultDialogController and ResultBackgroundViewController. The call to retain is not necessary when you are calling initWithNibName:bundle:. Perhaps you are doing this because you some crash when you released the controllers?

What you should do is to create ivars and properties for your controllers.

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

Then when showing the controllers you can do something like:

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

Then in buttonTapped: you do:

[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]; 
     }
 ];

And to top it off, don't forget to release the controller ivars in dealloc.


You can hide them by setting HIDE property for the views to be true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜