如何修改NSNotificationCenter addObsever方法的实现
来源:互联网 发布:c语言中使用thumb指令 编辑:程序博客网 时间:2024/04/28 12:57
如何修改NSNotificationCenter addObsever方法的实现,首先想到的是继承一个子类自己实现方法.但是项目进行了很久,以前的没有用这个子类,要兼容以前的肯定不能这样写,最后想到用cateory+runtime方法直接替换addObsever方法.就如这样:
+(void)load {
[selfswitchMethod];
}
- (void)addAvoidDuplicationObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject {//避免重复添加通知
[self removeObserver:observername:aName object:anObject];
[self addAvoidDuplicationObserver:observerselector:aSelector name:aName object:anObject];
}
+ (void)switchMethod
{
SEL addSel = @selector(addObserver:selector:name:object:);
SEL myAddSel = @selector(addAvoidDuplicationObserver:selector:name:object:);
Method systemRemoveMethod = class_getInstanceMethod([self class],addSel);
Method DasenRemoveMethod = class_getInstanceMethod([self class], myAddSel);
method_exchangeImplementations(systemRemoveMethod, DasenRemoveMethod);
}
- 如何修改NSNotificationCenter addObsever方法的实现
- iOS: NSNotificationCenter的方法defaultCenter
- iOS: NSNotificationCenter的方法postNotification:
- iOS: NSNotificationCenter的方法removeObserver:
- iOS: NSNotificationCenter的方法postNotificationName:object:
- iOS: NSNotificationCenter的方法postNotificationName:object:userInfo:
- iOS: NSNotificationCenter的方法removeObserver:name:object:
- iOS: NSNotificationCenter的方法removeObserver:
- NSNotificationCenter 释放的两种方法的利弊
- 手动实现NSNotificationCenter功能
- iOS: NSNotificationCenter的方法addObserver:selector:name:object:
- iOS: NSNotificationCenter的方法addObserverForName:object:queue:usingBlock:
- NSNotificationCenter没有处理好带来的通知方法执行多次
- 网卡MAC地址相关信息大全-3 如何实现修改网卡物理地址的三种方法
- 关于NSNotiFication和NSNotificationCenter的原理及实现
- 如何修改MAC地址的方法
- Python 实现str类型修改的方法
- PHP修改图片大小的实现方法
- Android 下陀螺仪、加速度和磁场传感器数据定义
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- ViewPager
- 回调方法介绍之中国好室友篇(Java示例)
- 14.3.2.3 Consistent Nonlocking Reads 一致性非锁定读
- 如何修改NSNotificationCenter addObsever方法的实现
- PyCharm中使用virtualenv进行django开发
- 数据结构与算法:排序
- appium自动测试化相关
- mybatis和hibernate创建会话的简单区别
- 在设置zookeeper集群中出现的问题
- CentOS系统下使用maven编译javaCPP-ffmpeg
- RecyclerView 实现瀑布流交错效果,并使最后一行子View高度占满RecyclerView
- 一个隐藏多年的bug