如何修改系统方法

iOS SDK中的方法只有在头文件声明,实现在一系列的框架中,我们并不知道具体是如何实现的。

有时候我们想实现不同于系统方法的效果,有人会说自己写一个方法就是了,不调用系统的方法,这样也行;又有的时候还是想实现系统方法的效果,但还想额外添加一些功能,这时又有人会说调完系统方法之后再添加就是了。如果添加的额外功能不一致,那也只能用这种办法,如果添加的都是相同的功能,那不是每调用完系统方法后都要写重复的代码?

那如何才能既不写那么多重复的代码,又能在系统方法上添加额外的功能,本文将要讲的就是如何解决该问题——修改系统方法。


在哪修改

一开始也说了,系统方法的实现都在一系列框架中,都已封装好,我们无法看到,更不能修改,这个时候就要用到Objective-C 的Category,你肯定知道将要修改的方法是属于那个类的,创建该类的Category。我们将在该类的Category中修改。



如何修改

说白了其实就是自己写一个方法来替换系统的方法,但调用的时候还是调用的系统方法的名字,实现其实是按照你自定义方法的实现,听上去不是很明白,下面看具体如何操作吧。

1.确定修改的方法

首先确定要修改的方法,你会说上面创建Category的时候不就确定过了吗,好吧,上面说的是OC方法,这里需要转换一下,1和2都是在类方法  + (void)load 中进行。
    SEL originalSelector_present = @selector(presentViewController:animated:completion:);
    SEL replaceSelector_present = @selector(vj_presentViewController:animated:completion:);
    
    Method originalMethod_present = class_getInstanceMethod([UIViewController class], originalSelector_present);
    Method replaceMethod_present = class_getInstanceMethod([UIViewController class], replaceSelector_present);


2.交换方法

method_exchangeImplementations(originalMethod_present, replaceMethod_present);


3.实现替换方法

从上面可以看到 vj_presentViewController:animated:completion: 就是我们替换的方法,在该方法里写具体的实现代码就可以了。
- (void)vj_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
    [self vj_presentViewController:viewControllerToPresent animated:flag completion:completion];
    //Custom Code



}


也许你会问替换方法里为什么又调自己,这不是无限循环下去了吗?其实不然,上面我们不是已经交换了吗,所以这里的 [self vj_presentViewController:viewControllerToPresent animated:flag completion:completion]; 其实调的是系统的presentViewController:viewControllerToPresent animated:flag completion:completion 方法 ,这样就能合理的解释你在别处调用系统的该方法其实调的是你自定义的替换方法。

4.详细代码

#import "UIViewController+AddPresentController.h"

@implementation UIViewController (AddPresentController)

+ (void)load
{
    SEL originalSelector_present = @selector(presentViewController:animated:completion:);
    SEL replaceSelector_present = @selector(vj_presentViewController:animated:completion:);
    
    Method originalMethod_present = class_getInstanceMethod([UIViewController class], originalSelector_present);
    Method replaceMethod_present = class_getInstanceMethod([UIViewController class], replaceSelector_present);
    method_exchangeImplementations(originalMethod_present, replaceMethod_present);
    
    SEL originalSelector_dismiss = @selector(dismissViewControllerAnimated:completion:);
    SEL replaceSelector_dismiss = @selector(vj_dismissViewControllerAnimated:completion:);
    
    Method originalMethod_dismiss = class_getInstanceMethod([UIViewController class], originalSelector_dismiss);
    Method replaceMethod_dismiss = class_getInstanceMethod([UIViewController class], replaceSelector_dismiss);
    method_exchangeImplementations(originalMethod_dismiss, replaceMethod_dismiss);
}

- (void)vj_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
    [self vj_presentViewController:viewControllerToPresent animated:flag completion:completion];
    //Custom Code


}

- (void)vj_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [self vj_dismissViewControllerAnimated:flag completion:completion];
    //Custom Code


}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值