addObserver forKeyPath options 注意事项

[self.layer addObserver:self forKeyPath:@"cornerRadius" options:NSKeyValueObservingOptionNew context:nil];

添加一个监听

在运行过程中可能出现错误

EXC_BAD_ACCESS

这是因为没有在dealloc中removeObserver的问题

但是有时不确定一个监听有没有添加时,

在dealloc进行removeObserver时又会报

because it is not registered as an observer.

这是因为穿点监听没有添加

但IOS不提供addObserver是否添加的判断功能

所以这里最好写为

@try

{

[self.layer removeObserver:self forKeyPath:@"cornerRadius"];

}

@catch(id exception){}

posted @ 2015-11-27 12:03  张永存(Jerry)  阅读(1930)  评论(0编辑  收藏  举报