杂记

杂记

交换方法
+ (void)exchangeInstanceMethod:(Class)anClass method1Sel:(SEL)method1Sel method2Sel:(SEL)method2Sel {

 Method originalMethod = class_getInstanceMethod(anClass, method1Sel);

 Method swizzledMethod = class_getInstanceMethod(anClass, method2Sel);

 BOOL didAddMethod =

 class_addMethod(anClass,

 method1Sel,

 method_getImplementation(swizzledMethod),

 method_getTypeEncoding(swizzledMethod));

 if (didAddMethod) {

 class_replaceMethod(anClass,

 method2Sel,

 method_getImplementation(originalMethod),

 method_getTypeEncoding(originalMethod));

 }

 else {

 method_exchangeImplementations(originalMethod, swizzledMethod);

 }
}
BOOL didAddMethod =

 class_addMethod(anClass,

 method1Sel,

 method_getImplementation(swizzledMethod),

 method_getTypeEncoding(swizzledMethod));

给类添加方法1的方法名方法2的方法实现和参数,如果返回成功,那就说明类不存在方法1,直接给类添加了新方法名为方法1方法实现为方法2的方法,然后将类中方法名为方法2的方法的实现替换为方法1的实现;如果返回为NO,证明类中存在方法名为方法1的方法,下面进行交换

响应者链逆向传值

响应者链是从UIApplication->UIViewController->UIView->TableView->cell
cell是第一响应者,所谓的逆向就是从第一响应者到第二响应者或者第三响应者的传值。对层view层较多的情况非常有效。

  1. 给添加UIResponder添加分类
  2. 添加方法 - (void)userInterfaceAction:(NSString *)eventName userInfo:(NSDictionary *)userInfo
  3. 实现方法
- (void)userInterfaceAction:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if (self.nextResponder) {
        [self.nextResponder userInterfaceAction:eventName userInfo:userInfo];
    }
}

在需要调用的地方直接调用分类方法,根据eventName参数进行区分(比如在cell中),在需要拦截的地方也实现方法,在方法中进行处理。(比如UIViewController中)。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • javascript功能插件大集合,写前端的亲们记得收藏 包管理器管理着 javascript 库,并提供读取和打...
    狗狗嗖阅读 817评论 0 1
  • cookie和缓存 Session是由应用服务器维持的一个服务器端的存储空间,用户在连接服务器时,会由服务器生成一...
    纵我不往矣阅读 435评论 0 1
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 问答题 iOS的开发和发布签名证书有何异同?开发签名证书绑定App ID、开发者证书、以及测试设备,用于真机测试。...
    Sparkle_S阅读 1,082评论 0 1
  • swift 中所有的数据类型都是由三种不同的数据结构(枚举、结构体、类)中的某一种实现的。 枚举和结构体在代码中以...
    August24阅读 214评论 0 0