我有一个UITableView.
在这里我有不同的单元格.每个单元格都有一个模型.使用KVO和NotificationCenter,单元格会收听模型以进行更改.当我离开ViewController我得到这个错误:
An instance 0x109564200 of class Model was deallocated while key value observers were still registered with it. Observation info was leaked,and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: <NSkeyvalueObservationInfo 0x109429cc0> ( <NSkeyvalueObservance 0x109429c50: Observer: 0x10942d1c0,Key path: name,Options: <New: NO,Old: NO,Prior: NO> Context: 0x0,Property: 0x10968fa00> )
在单元格中,当模型属性设置/更改时,我执行此操作:
[_model addobserver:self forKeyPath:@"name" options:0 context:nil]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(modelIsInvalid:) name:@"modelIsInvalid" object:_model];
然后在单元格的dealloc中:
- (void)dealloc { NSLog(@"DEALLOC CELL"); [[NSNotificationCenter defaultCenter] removeObserver:self]; [_model removeObserver:self forKeyPath:@"name"]; }
在模型中,我还检查它什么时候被释放:
- (void)dealloc { NSLog(@"DEALLOC MODEL"); }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。