===========================这是在一个类里面的使用=============================================
//这里是添加一个button 触发点击事件 去调用
UIButton * button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button addTarget:self action:@selector(didwo) forControlEvents:UIControlEventTouchUpInside];button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"maomao" forState:UIControlStateNormal];
[self.window addSubview:button];
//这是上面button的点击事件 就是回调NSInvocation的方法
-(void)didwo{
//要调用的方法
SEL selector = @selector(myMethod:withParameter2:);
//方法签名类
NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selector];
//初始化invocation
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
//设置target
[invocation setTarget:self];
//设置调用方法
[invocation setSelector:selector];
NSString *returnValue = nil; // 这是接受
NSString *argument1 = @"我是毛毛"; // 传过去的第一个参数
NSNumber *argument2 = [NSNumber numberWithInt:33]; // 传过去的第二个参数
//下表是从2开始的因为target和selector占用了0跟1
[invocation setArgument:&argument1 atIndex:2];
[invocation setArgument:&argument2 atIndex:3];
//记录所有的参数防止释放找不到
[invocation retainArguments];
//消息调用
[invocation invoke];
//得到消息
[invocation getReturnValue:&returnValue];
NSLog(@"Return Value = %@", returnValue);
}
这是上面回调的方法
- (NSString *) myMethod:(NSString *)parameter1 withParameter2:(NSNumber *)parameter2{
NSString *result = @"Objective-C";
NSLog(@"Param 1 = %@", parameter1);
NSLog(@"Param 2 = %@", parameter2);
return(result);
}
这是点击button后的打印结果
2014-06-24 15:34:49.371 激光推送[27163:60b] Param 1 = 我是毛毛
2014-06-24 15:34:49.371 激光推送[27163:60b] Param 2 = 33
2014-06-24 15:34:49.372 激光推送[27163:60b] Return Value = Objective-C
===========================两个类的使用===================================
#import <Foundation/Foundation.h>
@interface MM : NSObject
-(BOOL)myMethod:(NSString *)parmeter withParameter2:(NSString *)parameter2;
#import "MM.h"
@implementation MM
-(BOOL)myMethod:(NSString *)parmeter withParameter2:(NSString *)parameter2{
return [parmeter compare:parameter2];
}
UIButton * button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button addTarget:self action:@selector(didwo) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"maomao" forState:UIControlStateNormal];
[self.view addSubview:button];
-(void)didwo{
//方法签名类,就是要被调用的类
MM * mm = [[MM alloc] init];
NSMethodSignature * sig = [[mm class] instanceMethodSignatureForSelector:@selector(myMethod:withParameter2:)];
//初始化NSIvocation并且带一个signature
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:sig];
//设置target调用类 这里的target是你调用方法的那个类 不要写错写错会崩
[invocation setTarget:mm];
//这是被调用的方法
[invocation setSelector:@selector(myMethod:withParameter2:)];
//如果有参数传入就需要者样去传参数,index从2开始,因为0和1被target和selector占用
NSString * string1 = @"毛毛"; // 这里的参数就可以网络请求的参数或者你别的参数 都可以
NSString * string2 = @"雷雷"; // 这就看你想怎么用了
[invocation setArgument:&string1 atIndex:2];
[invocation setArgument:&string2 atIndex:3];
//retain所有的参数 以防被释放
[invocation retainArguments];
//消息调用
[invocation invoke];
//获得返回值类型
const char *returnType = sig.methodReturnType;
//声明返回值变量
id returnValue;
//这里的判断是为了安全起见
//如果没有返回值,也就是消息声明为void,那么returnValue=nil
if( !strcmp(returnType, @encode(void)) ){
returnValue = nil;
}
//如果返回值为对象,那么为变量赋值
else if( !strcmp(returnType, @encode(id)) ){
[invocation getReturnValue:&returnValue];
}
else{
//如果返回值为普通类型NSInteger BOOL
//返回值长度
NSUInteger length = [sig methodReturnLength];
//根据长度申请内存
void *buffer = (void *)malloc(length);
//为变量赋值
[invocation getReturnValue:buffer];
//
if( !strcmp(returnType, @encode(BOOL)) ) {
returnValue = [NSNumber numberWithBool:*((BOOL*)buffer)];
}
else if( !strcmp(returnType, @encode(NSInteger)) ){
returnValue = [NSNumber numberWithInteger:*((NSInteger*)buffer)];
}
returnValue = [NSValue valueWithBytes:buffer objCType:returnType];
}
NSLog(@"%@",returnValue);
}