您的位置:首页 > 移动开发 > IOS开发

如何修改系统方法

2014-01-06 16:49 726 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息