SEL sel = @selector (start:) ; // 指定action
if ([obj respondsToSelector:sel])
{ //判断该对象是否有相应的方法
[obj performSelector:sel withObject:self]; //调用选择器方法
}
使用[[UIApplication sharedApplication] keyWindow]查找应用程序的主窗口对象
respondsToSelector判断是否实现了某方法
#import < Foundation / >
@interface Tester : NSObject {
}
- ( void ) test:(NSString * ) msg;
- ( void ) notImp;
@end
#import " "
@implementation Tester
- ( void ) test:(NSString * ) msg
{
NSLog( @" %@ " , msg);
}
@end
#import < Foundation / >
#import " "
int main ( int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id tester = [[Tester alloc] init]; // 注意,这里使用id
SEL testSelector = @selector(test:);
SEL notImpSelector = @selector(notImp:);
if ([tester respondsToSelector:testSelector])
{
// 中实现了test方法
[tester test: @" invoke test method " ];
}
if ([tester respondsToSelector:notImpSelector])
{
// 中没有实现此主就去
[tester notImp];
}
[pool drain];
return 0 ;
}