当 UIView 与其 superView 分离时如何收到通知?

发布于 2024-09-06 02:46:55 字数 145 浏览 4 评论 0原文

看来UIView没有像“didRemoveFromSuperview”或“willRemoveFromSuperview”这样的方法。那么,当UIView从其superView中删除时如何监听事件?我应该使用科沃?提前致谢!

It seems that the UIView has not methods like "didRemoveFromSuperview" or "willRemoveFromSuperview".Then,How to listen to the event when a UIView removed from its superView?I should use KVO? thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘸点软妹酱 2024-09-13 02:46:55

这有效(在 iOS8 上测试):

-(void) didMoveToWindow {
    [super didMoveToWindow]; // (does nothing by default)
    if (self.window == nil) {
        // YOUR CODE FOR WHEN UIVIEW IS REMOVED
    }
}

根据 UIView 文档

此方法的默认实现不执行任何操作。子类可以重写它,以便在窗口更改时执行其他操作。

窗口属性可能为nil...当接收者刚刚从其超级视图中删除时,或者当接收者刚刚添加到一个超级视图中时,就会发生这种情况未连接到窗口。

This works (tested on iOS8):

-(void) didMoveToWindow {
    [super didMoveToWindow]; // (does nothing by default)
    if (self.window == nil) {
        // YOUR CODE FOR WHEN UIVIEW IS REMOVED
    }
}

According to the UIView docs:

The default implementation of this method does nothing. Subclasses can override it to perform additional actions whenever the window changes.

The window property may be nil... This occurs when the receiver has just been removed from its superview or when the receiver has just been added to a superview that is not attached to a window.

流年里的时光 2024-09-13 02:46:55

这个主题已经很老了,但是我找到了一种方法。由于谷歌搜索不够有用,所以这里是(取自 UIView 的文档)

观察与视图相关的变化

--didAddSubview:

– willRemoveSubview:

- 将移动到超级视图:

– didMoveToSuperview

- 将移动到窗口:

– didMoveToWindow

This topic is quite old, but I found a way to do it .Since google search wasn't helpful enough, here it is (taken from UIView's docs)

Observing View-Related Changes

– didAddSubview:

– willRemoveSubview:

– willMoveToSuperview:

– didMoveToSuperview

– willMoveToWindow:

– didMoveToWindow

深者入戏 2024-09-13 02:46:55
- (void) willMoveToSuperview: (UIView *) newSuperview{
    if(newSuperview == nil){
        // UIView was removed from superview
    } else {
        // UIView was added to superview
    }
}
- (void) willMoveToSuperview: (UIView *) newSuperview{
    if(newSuperview == nil){
        // UIView was removed from superview
    } else {
        // UIView was added to superview
    }
}
断爱 2024-09-13 02:46:55

您可以对 UIView 进行子类化,并通过其 - (void)removeFromSuperview 方法发布通知。

You can subclass your UIView and post notifications from it's - (void)removeFromSuperview method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文