RunTime(消息機(jī)制) + RunTime(消息轉(zhuǎn)發(fā))
一.消息機(jī)制
1.在viewDidLoad中直接用 performSelector:@selector(doSomething) 來調(diào)用doSomething方法時(shí),會(huì)發(fā)現(xiàn)找不到這個(gè)方法而奔潰.此時(shí),我們可以在resolveInsantanceMethod:(SEL)see 方法中獲取這個(gè)所有在運(yùn)行時(shí)階段的方法,在這個(gè)方法中只需要判斷一下,將這個(gè)方法獲取,并且運(yùn)用Runtime 的 class_addMethod 的方法來將方法和響應(yīng)函數(shù)綁定,進(jìn)而達(dá)到為某一個(gè)類添加方法的目的.
- (void)viewDidLoad {
[superviewDidLoad];
[selfperformSelector:@selector(doSomething)];
[superviewDidLoad];
[selfperformSelector:@selector(doSomething)];
}
//1.對象在收到無法解讀的消息后,會(huì)調(diào)用resolveInstanceMethod
//2.再用class_addMethod 將調(diào)用的方法名字和制定函數(shù)相關(guān)聯(lián)
+ (BOOL)resolveInstanceMethod:(SEL)sel {
if (sel == @selector(doSomething)) {
NSLog(@"add method here");
class_addMethod([selfclass], sel, (IMP)dynamicMethodIMP, "v@:");
returnYES;
}
return [superresolveInstanceMethod:sel];
NSLog(@"add method here");
class_addMethod([selfclass], sel, (IMP)dynamicMethodIMP, "v@:");
returnYES;
}
return [superresolveInstanceMethod:sel];
}
//函數(shù)
void dynamicMethodIMP (idself,SEL_cmd) {
NSLog(@"doSomething SEL");
NSLog(@"doSomething SEL");
}
+ (BOOL)resolveClassMethod:(SEL)sel {
NSLog(@">> Class resolving %@",NSStringFromSelector(sel));
return [superresolveClassMethod:sel];
NSLog(@">> Class resolving %@",NSStringFromSelector(sel));
return [superresolveClassMethod:sel];
}
二.消息轉(zhuǎn)發(fā)
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfperformSelector:@selector(messageViewMethod)];
}
/**
* @author zhouyantong
*
* @brief 在這個(gè)方法中并沒有找到或者實(shí)現(xiàn)相應(yīng)的方法,那么消息會(huì)往下傳遞,在轉(zhuǎn)發(fā)方法中看別的界面是否有這個(gè)方法
*
*/
+ (BOOL)resolveInstanceMethod:(SEL)sel {
return [superresolveInstanceMethod:sel];
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
Class class = NSClassFromString(@"MessageViewController");
UIViewController *vc = class.new;
if (aSelector == NSSelectorFromString(@"messageViewMethod")) {
NSLog(@"MessageVC do this!");
return vc;
}
returnnil;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfperformSelector:@selector(messageViewMethod)];
}
/**
* @author zhouyantong
*
* @brief 在這個(gè)方法中并沒有找到或者實(shí)現(xiàn)相應(yīng)的方法,那么消息會(huì)往下傳遞,在轉(zhuǎn)發(fā)方法中看別的界面是否有這個(gè)方法
*
*/
+ (BOOL)resolveInstanceMethod:(SEL)sel {
return [superresolveInstanceMethod:sel];
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
Class class = NSClassFromString(@"MessageViewController");
UIViewController *vc = class.new;
if (aSelector == NSSelectorFromString(@"messageViewMethod")) {
NSLog(@"MessageVC do this!");
return vc;
}
returnnil;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "MessageViewController.h"
@interfaceMessageViewController ()
@end
@implementation MessageViewController
- (void)viewDidLoad {
}
- (void)messageViewMethod {
NSLog(@"This is receive method!");
}
@interfaceMessageViewController ()
@end
@implementation MessageViewController
- (void)viewDidLoad {
}
- (void)messageViewMethod {
NSLog(@"This is receive method!");
}
@end

浙公網(wǎng)安備 33010602011771號