iOS-设计模式之通知

通知设计模式简单好用,就是一个项目中如果用的太多,不利于代码维护,可读性太差。

实现过程:

    [[NSNotificationCenter defaultCenter]postNotificationName:@"notificarions" object:self userInfo:dic];

发一个通知,可以在userInfo:这里进行传值,

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificarions:) name:@"notificarions" object:nil];

接收通知,一定要注意通知名一定要一样。

最后不要忘记要移除通知,否则回造成内存泄漏

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notificarions" object:nil];

本文GitHub地址https://github.com/zhangkiwi/iOS_SN_NSNotificationCenter

原文地址:https://www.cnblogs.com/zhang-kiwi/p/5018120.html