- 浏览: 762916 次
- 性别:
- 来自: 天堂
文章分类
最新评论
-
xiaozhao-521:
呀呀呀呀呀呀呀
RequestTest222 -
Andy_hyh:
打扰了,问下openmeeting源码可以运行起来吗?
Openmeetings安装 详细步骤 -
qindongliang1922:
擦,现在还行么,厉害
北京免费吃饭的地方 -
minixx77:
...
Openmeetings安装 详细步骤 -
wwwqqqiang:
喜欢楼主分享问题的方式,有思想
UIView 和 CALayer的那点事
在ios直接调用某个对象的消息是方法有两种:
一:performselector:withObject:
二:invocation
第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作
NSInvocation可以处理参数、返回值。会java的人都知道凡是操作,其实NSInvocation就相当于反射操作。
//方法签名类,需要被调用消息所属的类AsynInvoke ,被调用的消息invokeMethod: NSMethodSignature *sig= [[AsynInvoke class] instanceMethodSignatureForSelector:@selector(invokeMethod:)]; //根据方法签名创建一个NSInvocation NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:sig]; //设置调用者也就是AsynInvoked的实例对象,在这里我用self替代 [invocation setTarget:self]; //设置被调用的消息 [invocation setSelector:@selector(invokeMethod:)]; //如果此消息有参数需要传入,那么就需要按照如下方法进行参数设置,需要注意的是,atIndex的下标必须从2开始。原因为:0 1 两个参数已经被target 和selector占用 NSInteger num=10; [invocation setArgument:&num atIndex:2]; //retain 所有参数,防止参数被释放dealloc [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]; }
调用步骤:
- (NSString *) myMethod:(NSString *)param1 withParam2:(NSNumber *)param2{ NSString *result = @"Objective-C"; NSLog(@"Param 1 = %@", param1); NSLog(@"Param 2 = %@", param2); return(result); } - (void) invokeMyMethodDynamically { SEL selector = @selector(myMethod:withParam2:); NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selector]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; [invocation setTarget:self]; [invocation setSelector:selector]; NSString *returnValue = nil; NSString *argument1 = @"First Parameter"; NSNumber *argument2 = [NSNumber numberWithInt:102]; [invocation setArgument:&argument1 atIndex:2]; [invocation setArgument:&argument2 atIndex:3]; [invocation retainArguments]; [invocation invoke]; [invocation getReturnValue:&returnValue]; NSLog(@"Return Value = %@", returnValue); }
To do this, you need to follow these steps:
1. Form a SEL value using the name of the method and its parameter names (as
explained in Recipe 1.7).
2. Form a method signature of type NSMethodSignature out of your SEL value.
3. Form an invocation of type NSInvocation out of your method signature.
4. Tell the invocation what object you are targeting.
5. Tell the invocation what selector in that object you want to invoke.
6. Assign any arguments, one by one, to the invocation.
7. Invoke the method using the invocation object and demand a return value (if any).
发表评论
-
iOS 自定义UIActionSheet
2012-12-18 16:07 16373一:模态视图 UIActi ... -
UIView 和 CALayer的那点事
2012-11-17 23:51 30620UIView 和 CALayer的那点事 (1 ... -
iOS Open Source : Popover API for iPhone
2012-01-20 15:02 1899http://iphonedevelopertips.com/ ... -
ios 任务、线程、定时器
2011-12-26 18:09 7974一:operations(任务) cocoa提供了三种 ... -
ios url缓存策略——NSURLCache、 NSURLRequest
2011-12-26 17:09 24306一:url 缓存策略 NSURLRequest ... -
iphone 对Web Services的三种请求方式soap get post
2011-11-09 10:57 6370一:Using SO AP 1.1 POST / ... -
sdk3.2手势实例
2011-11-09 10:11 1702#import <UIKit/UIKit.h>@i ... -
关于iphone 利用hpple解析html的问题
2011-08-04 18:28 2168最近在用happe解析html中的图片。有个翻页操作,如果请 ... -
iphone hpple 解析html,xml
2011-07-19 16:21 2705使用Objective-C解析HTML或者XML,系统自带有两 ... -
激活 iPhone通过 GPRS 连接服务器功能的代码
2011-05-13 15:14 1609如果您的 iPhone 应用里含有连接服务器的功能,也许会遇到 ... -
address book api 图型
2011-04-28 15:51 1102最近要搞地址簿了,整理一下 -
[OmniGraffle]iPhone app原型制作工具
2011-04-06 17:35 3910在写程序之前,我们通常需要做一些mockup出来(不知道款爷有 ... -
自定义uislider 样式
2011-04-04 21:28 3789UIImage *stetchLeftTrack= [[UII ... -
iphone 下AsyncSocket网络库编程
2011-04-02 21:04 7588iphone的标准推荐CFNetwork ... -
进阶AlertView运用 - 登入设计
2011-04-01 17:52 2992说明:示范如何利用AlertView来制作系统登入的介面程式碼 ... -
iPad UIPopoverController弹出窗口的位置和坐标
2011-04-01 17:42 1955优化规则: TodoViewControlle ... -
iPhone系统自动化测试
2011-04-01 17:39 2571首先mac系统是必备的2 安装iPhone SD ... -
iphone上面编写具有root权限的程序
2011-04-01 17:31 6240正常途径下, 我们编写的程序发布在App store上, 使用 ... -
聊天。。。。。
2011-04-01 17:13 1050是得分手段 -
iOS开发基础:Modal View Controller的不同呈现方式
2011-04-01 16:40 2772ModalViewController可以有不同的呈现方式(m ...
相关推荐
但是对于>2个的参数或者有返回值的处理,那performSelector:withObject就显得有点有心无力了,那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作 NSInvocation的基本使用 方法签名类 // 方法...
策略19.1 何为策略模式19.2 何时使用策略模式19.3 在UITextField中应用验证策略19.4 总结第20章 命令20.1 何为命令模式20.2 何时使用命令模式20.3 在Cocoa Touch框架中使用命令模式20.3.1 NSInvocation对象20.3.2 ...
NSInvocation的作用和performSelector:withObject:的作用是一样的:用于iOS编程中调用某个对象的消息。 performSelector:withObject:调用一些参数较少的消息是比较方便的,但是对于参数个数大于2的消息,使用...
ISInvocationHookProxy 挂钩目标的每个 NSInvocation 的代理对象。用法 NSString *object = [[ NSString alloc ] init ];ISInvocationHookProxy *proxy = [[ISInvocationHookProxy alloc ] initWithTarget: object];...
尽管NSOperation和NSOperationQueue在某些重复性问题上很好地工作,而在其他问题上则可以使用NSInvocation ,但iOS并未真正包含一组用于轻松管理大量任意背景任务的工具。 EDQueue提供了一个高级接口,用于使用和...
,下面将给大家详细介绍关于iOS定时器使用的相关内容,话不多说了,来一起看看详细的介绍吧。 一. NSTimer NSTimer的初始化方法有以下几种: 会自动启动, 并加入 MainRunloop 的 NSDefaultRunLoopMode 中, 注意: ...
DesignPatterns-iOS 使用设计模式的示例应用程序。 在此示例中,我使用了: MVC:模型视图控制器。 单例:在整个应用程序中仅创建一个实例。 外观:到复杂子系统的单一接口。 装饰器:在不修改对象代码的情况下...
深入理解iOS热修复原理背景顾名思义热bug就是使App有了线上bug的能力,但是遗憾的是苹果出于安全的考虑考虑了热修复。虽然App审核加快了,但是依然无法很好的控制线上bug的影响范围。 JSPatch存在审核风险,所以我们...
iOS runtime forwardInvocation详解 代码: TestModel - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { if(aSelector == @selector(testMethod)) { return [NSMethodSignature ...
1.创建一个扩展UITableViewAdapter的适配器,覆盖将要使用的委托 2.然后设置UITableView [ _tableView setAdapter : [ self adapter ] ] 3.接下来覆盖tableView的超类中的func - ( void ) rountEvent : ( NSString...
_objc_msgForward_demo用来测试消息转发机制对象查找selector时,先查找cachelist,如果没有则查找methodlist,如果还没有就查找父类的methodlist都没有是... - (void)forwardInvocation:(NSInvocation *)anInvocati
CWCoreData ===========...NSInvocation还具有其他功能,以支持在线程边界上通过NSManagedObject实例进行调用。 创建该项目的目的是用作其他Xcode项目的子项目。 另外,该项目可以用作git子模块。 这个项目有一个独立的
OCMethodTrace:跟踪任何Objective-C方法调用