一、简介
NsInvocation 主要是为完成,调用某个对象的方法 ,进行使用的;(通常是因为这个对象的方法没有Api)
二、使用方式
Nsivlcation 的功能较为强大,可以传值多个参数;
在.m 文件中的代码
- (void)viewDidLoad {
[super viewDidLoad];
//NSMethodSignature: 生成的签名 第一个参数传的是你要调用的类,第二个参数是在类中实现的方法(方法不在.h文件中声明 也没有关系)
SEL seclector = NSSelectorFromString(@"changeName:withtype:");
NSMethodSignature * signature = [ViewController instanceMethodSignatureForSelector:seclector];
//创建NSInvocation 第一个参数传的是签名
NSInvocation * vocation = [NSInvocation invocationWithMethodSignature:signature];
//target传的是你想要调用的类 必须与生成签名的时候 一致
vocation.target = self;
//voation中的方法必须和签名中的方法一致
vocation.selector = seclector;
//参数的传递
NSString * name = @"郭晓广";
//设置参数的时候 不能从0开始,因为0已经被self占用,1已经被_cmd占用 只能从2开始
[vocation setArgument:&name atIndex:2];
//同上
NSString * type = @" 啊啊啊啊";
[vocation setArgument:&type atIndex:3];
//只要调用invocation的invoke方法,就代表执行nsivocation对象中制定对象的指定方法
[vocation invoke];
}
-(void)changeName:(NSString * )name withtype:(NSString*)type
{
NSLog(@"改变名字%@,%@",name,type);
}