ios - 当 UIView 从它的 superView 分离时如何得到通知?

标签 ios iphone uiview key-value-observing

好像UIView没有"didRemoveFromSuperview"或者"willRemoveFromSuperview"这样的方法。那么,如何监听UIView从它的superView中移除的事件呢?我应该使用 KVO?提前致谢!

最佳答案

这有效(在 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.

关于ios - 当 UIView 从它的 superView 分离时如何得到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3074933/

相关文章:

ios - verseGeocodeLocationCompletionHandler 失败并显示 "[TNSDictionaryAdapter coordinate]: unrecognized selector sent to instance"

ios - 为什么动画组合比例和翻译 CGAffineTransform 不能简单地工作?

ios - 如何在 UILabel 中设置字符和行之间的空格

iphone - 如何在文档目录中创建 plist 的精确副本

iphone - 从 View B 移回 View A 时如何重置屏幕方向?

iphone - 如何在加载了 nib 文件的 UIView 中调整 subview 的大小

objective-c - 动态调用带参数的方法

iphone - 如何在 objective-c 中使在方法中创建的对象保持事件状态?

ios - 如何在 UIView 类中编写自定义选择器?

ios - 在标签栏和标签栏项之间添加 subview