Cocoa内置只支持两个参数,要超过两个参数以上怎么办呢,下面代码展示了如何实现一个自己的方法来调用超过7个参数(来自three20)
- (ID)performSelector:(SEL)selector withObject:(ID)p1 withObject:(ID)p2 withObject:(ID)p3 withObject:(ID)p4 withObject:(ID)p5 withObject:(ID)p6 withObject:(ID)p7
{
NSMethodSignature *sig = [self methodSignatureForSelector:selector];
if (sig)
{ NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
[invo setTarget:self];
[invo setSelector:selector];
[invo setArgument:&p1 atIndex:2];
[invo setArgument:&p2 atIndex:3];
[invo setArgument:&p3 atIndex:4];
[invo setArgument:&p4 atIndex:5];
[invo setArgument:&p5 atIndex:6];
[invo setArgument:&p6 atIndex:7];
[invo setArgument:&p7 atIndex:8];
[invo invoke];
if (sig.methodReturnLength) {
ID anObject;
[invo getReturnValue:&anObject];
return anObject; }
else { return nil; } }
else { return nil; } }
总结以上是内存溢出为你收集整理的ios如何用perform selector调用超过两个以上参数的方法全部内容,希望文章能够帮你解决ios如何用perform selector调用超过两个以上参数的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)