viewDidDisappear for UIView

2019-05-28 05:24发布

I've added a sub view like this in the main view:

    BTLPXYPad *XYPad = [[BTLPXYPad alloc] initWithFrame: CGRectMake (30, 10, 280, 460)];
    [window addSubview:XYPad];

done all my bits that i need to and then removed it using this in the BTLPXYPad class:

    [self removeFromSuperview];

What I need is to perform a task once it has gone. I know that with a UIViewController type class I could use viewDidDissapear but I can't seem to find the same thing for a UIView Type. Can anyone help please?

2条回答
何必那么认真
2楼-- · 2019-05-28 05:30
[self removeFromSuperview];

What I need is to perform a task once it has gone.

When you say [self removeFromSuperview], it is gone. There is a delay of just one runloop for it to look gone, but removing the view removes the view.

So the solution is just to proceed to your "task once it is gone":

[self removeFromSuperview];
// do your task

If it seems like the "do your task" code is in the wrong place - it isn't. The fact that you need the view to be notified when it is gone shows that your architecture was wrong to start with. A view shouldn't be performing any "task"; it is View in the Model-View-Controller structure. It just displays stuff. Your view controller is Controller; it is the one to do the task.

Nor should the Controller need to consult the view at this point, because the view should not have been storing any important data to begin with. Data is Model, and should already have been retrieve and stored by the Controller before this moment.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-28 05:47

To know when you a view has actually been removed you could implement didMoveToSuperview and check if the superview is now nil

- (void)didMoveToSuperview;
{
  [super didMoveToSuperview];

  if (!self.superview) {
    NSLog(@"Removed from superview");
  }
} 
查看更多
登录 后发表回答