自定义UIView怎么注册销毁NSNotification通知

问题描述:在使用天猫tangram框架后。部分组件自定义后会用到通知,但是在iOS 8 系统中,会崩溃?

原因分析:当对象挂掉后,要对应移除注册的通知。 否则当你重复执行发送通知的时候,在iOS8 系统以下,就会崩溃(其他系统暂时没有发现)。我们需要
在接受通知的类里移除通知。但是对于自定义的UIView类,该怎么操作呢?

方案解决:

//移除通知方法'
- (void)willMoveToWindow:(UIWindow*)newWindow {
    if(newWindow == nil) {
        // Will be removed from window, similar to -viewDidUnload.'
        // Unsubscribe from any notifications here.'
        [[NSNotificationCenterdefaultCenter] removeObserver:self];
    }
}
//添加通知方法'
- (void)didMoveToWindow {
    if(self.window) {
        // Added to a window, similar to -viewDidLoad.'
        // Subscribe to notifications here.'
        [PublicFunctionaddObserver:selfnotificationName:@"refreshTheOpPersonalActivity"selector:@selector(clearTheArryData) object:nil];
    }
}
posted @ 2018-06-27 10:07  yoowei  阅读(994)  评论(0编辑  收藏  举报