我编写iOS应用程序并使用imageStore库来延迟加载图像并将它们缓存在内存中.(https://github.com/psychs/imagestore)
在ViewController上我创建了imagestore实例:
imageStore = [ImageStore new];
imageStore.delegate = self;
Run Code Online (Sandbox Code Playgroud)
当图像加载成功时,imagestore调用委托方法
- (void)imageStoreDidGetNewImage:(ImageStore*)sender url:(NSString*)url
Run Code Online (Sandbox Code Playgroud)
在tableview上执行reloadData以重绘单元格.一切都很好.但是有问题:如果ViewController执行了卸载(返回导航控制器)并加载了图像,应用程序完成崩溃,因为imagestore调用了卸载ViewController的方法.
我尝试执行以下操作:1)在ViewController中,我将此代码放在viewDidUnload部分中:
imageStore.delegate = nil;
imageStore = nil;
Run Code Online (Sandbox Code Playgroud)
2)在imageStore中,我添加了对nil的检查:
if(delegate != nil) {
...call delegate method
}
Run Code Online (Sandbox Code Playgroud)
它可以工作,但无论如何都会定期崩溃.
D33*_*6h7 12
尝试将此代码放在dealloc部分.
imageStore.delegate = nil;
imageStore = nil;
Run Code Online (Sandbox Code Playgroud)
同样,if子句不是必需的,因为应用程序会忽略对nil对象的任何调用,所以如果你有这样的东西:
id delegate = nil;
[delegate callAnyMethod];
Run Code Online (Sandbox Code Playgroud)
对你的应用程序行为没有任何影响,另一方面,如果方法委托的调用是可选的,你应该确保委托响应选择器,这样的事情应该做的伎俩:
if([delegate conformsToProtocol:@protocol(yourProtocolName)] && [delegate respondsToSelector:@selector(imageStoreDidGetNewImage:url:)]) {
[delegate imageStoreDidGetNewImage:imageStore url:url];
}
Run Code Online (Sandbox Code Playgroud)
干杯!
| 归档时间: |
|
| 查看次数: |
9654 次 |
| 最近记录: |