iOS调用perform selector超过2个参数以上的方法
2016-06-12 10:18
441 查看
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; } }
- (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开发:编辑框格式化输入、删除、插入
- IOS之NSArray 中调用的方法详解
- iOS - 富文本AttributedString
- iOS平台下cookie的使用
- iOS ipv6 兼容
- iOS 笔记
- iOS小莲花Versions的SVN工具不能提交.a文件的解决方法
- iOS开发 引入支付宝需要注意的地方
- iOS9新特性(二)
- IPv6介绍及程序支持IPv6的障碍
- 关于 iOS socket 都在这里了
- iOS教你轻松打造瀑布流Layout
- iOS 独立开发记录(下)
- iOS 独立开发记录(上)
- 【代码笔记】iOS-通过颜色来生成一个纯色图片
- 【代码笔记】iOS-通过颜色来生成一个纯色图片
- AVCaptureDevice 曝光模式
- IOS后台机制分析
- iOS 下的相册与图片处理
- IOS加密方式