//封裝invacation能夠調用多個參數的方法 -(void)invacation { //1.建立一個MethodSignature,簽名中保存了方法的名稱,參數和返回值 //這個方法屬於誰,那麼就用誰來進行建立 //注意:簽名通常是用來設置參數和得到返回值的,和方法的調用沒有太大的關係 NSMethodSignature *signature = [ViewController instanceMethodSignatureForSelector:@selector(callWithNumber:andContext:withStatus:)]; /*注意不要寫錯了方法名稱 // NSMethodSignature *signature = [ViewController methodSignatureForSelector:@selector(call)]; */ //2.經過MethodSignature來建立一個NSInvocation //NSInvocation中保存了方法所屬於的對象|方法名稱|參數|返回值等等 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; /*2.1 設置invocation,來調用方法*/ invocation.target = self; // invocation.selector = @selector(call); // invocation.selector = @selector(callWithNumber:); // invocation.selector = @selector(callWithNumber:andContext:); invocation.selector = @selector(callWithNumber:andContext:withStatus:); NSString *number = @"10086"; NSString *context = @"下課了"; NSString *status = @"睡覺的時候"; //注意: //1.自定義的參數索引從2開始,0和1已經被self and _cmd佔用了 //2.方法簽名中保存的方法名稱必須和調用的名稱一致 [invocation setArgument:&number atIndex:2]; [invocation setArgument:&context atIndex:3]; [invocation setArgument:&status atIndex:4]; /*3.調用invok方法來執行*/ [invocation invoke]; }