NSInvocation直接调用某个对象的消息
来源:互联网 发布:广州网络批发服装市场 编辑:程序博客网 时间:2024/06/08 14:10
在 iOS中可以直接调用某个对象的消息方式有两种:
一种是performSelector:withObject;
再一种就是NSInvocation。
第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作。
main.h
1#import <Foundation/Foundation.h>2#import"MyClass.h"34intmain (int argc,constchar * argv[])5{67NSAutoreleasePool* pool=[[NSAutoreleasePoolalloc]init];89MyClass*myClass=[[MyClassalloc]init];10NSString*myString=@"My string";1112//普通调用13NSString*normalInvokeString=[myClassappendMyString:myString];14NSLog(@"The normal invoke string is: %@", normalInvokeString);1516//NSInvocation调用17SEL mySelector=@selector(appendMyString:); //
SEL selector =NSSelectorFromString(@"setStatusBarOrientation:"); 多种方式
18NSMethodSignature* sig=[[myClassclass]19instanceMethodSignatureForSelector: mySelector];2021NSInvocation* myInvocation=[NSInvocationinvocationWithMethodSignature: sig];22[myInvocationsetTarget: myClass];23[myInvocationsetSelector: mySelector];2425[myInvocationsetArgument:&myStringatIndex: 2];2627NSString* result=nil;28[myInvocationretainArguments];29[myInvocationinvoke];30[myInvocationgetReturnValue:&result];31NSLog(@"The NSInvocation invoke string is: %@", result);3233 35 36return0;37}MyClass.h
1#import <Foundation/Foundation.h>234@interfaceMyClass: NSObject { 5}67- (NSString*)appendMyString:(NSString*)string;89@end
MyClass.m
1#import"MyClass.h"234@implementationMyClass56 1516- (NSString*)appendMyString:(NSString*)string17{18NSString*mString=[NSStringstringWithFormat:@"%@ after append method",string];1920return mString;21}22
2728@end
2728@end
这里说明一下[myInvocation setArgument: &myString atIndex: 2];为什么index从2开始 ,原因为:0 1 两个参数已经被target 和selector占用。
在附上一个实例,解决了ipad 横屏时,进入app 右侧黑屏的问题
//解决横屏启动时,app右侧是黑的问题 if ([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarOrientation:)]){ SEL selector = NSSelectorFromString(@"setStatusBarOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIApplication instanceMethodSignatureForSelector:selector]]; UIDeviceOrientation orentation = UIDeviceOrientationPortrait; [invocation setSelector:selector]; [invocation setTarget:[UIApplication sharedApplication]]; [invocation setArgument:&orentation atIndex:2]; [invocation invoke]; }
0 0
- NSInvocation直接调用某个对象的消息
- iOS中可以直接调用 某个对象的消息
- NSInvocation使用示例 IOS 对象方法的调用 NSInvocation PK performSelector
- iOS消息机制 NSInvocation的深入理解
- 87.直接向对象发送消息和通过performSelector调用的区别
- iOS调用方法---NSInvocation的使用
- 网页直接调用android客户端的某个功能
- 直接获取某个组件的对象(this[]用法)
- 消息转发-NSInvocation和NSUndoManager的关系
- iOS 类的实例方法调用 NSInvocation performSelector
- NSMethodSignature、NSInvocation进行消息转发
- 类名直接调用与对象引用的区别
- NSInvocation简单使用 调用方法
- NSInvocation动态调用任意block
- [Flex]Flex编程注意之直接获取某个组件的对象(this[]用法)
- [Flex]Flex编程注意之直接获取某个组件的对象(this[]用法)
- HttpURLConnection 直接发送soap消息调用webservice
- HttpURLConnection 直接发送soap消息调用webservice
- GridView内Button获取不了onItemClickListener 及小说章数分页....
- linux自动同步时间的方法
- ELF格式
- SVN与Git
- window.onload 和$(document).ready()的区别
- NSInvocation直接调用某个对象的消息
- 常用的okhttp post上传方式
- 高并发量网站解决方案
- SVN之update版本冲突one or more files are in a conflicted state
- BZOJ2086: [Poi2010]Blocks
- Linux+树莓派3开发总结——cx_Freeze打包Python3程序(工程文件)
- linux 查看文件夹下的文件个数(当前目录的文件数
- iOS开发 适配iOS10以及Xcode8
- Android 性能分析工具整理汇总