1、定義:
SEL:類成員方法的指針;
2、本質:
在iOS中SEL本質就是:類方法的編號,函數的地址;
@selector() 實際上就是取得類方法的編號;
@selector() 他的行爲基本可以等同C語言的中函數指針;
@selector(xxxx)的作用是找到名字爲xxxx的方法。
3、使用:
[objA performSelector:@selector(funB)];
即調用objA對象的funB方法,和[objA
funB]的意思一樣,更加動態一些。
SEL setWidthHeight = @selector(setWidth:height:);
4、注意:
當SEL作爲屬性使用時,必須連接着SEL所指示的方法的上下句柄;
即SEL與id 一起使用;
@property (nonatomic,assign) SEL methodTest;
@property (nonatomic,retain) id handle; //添加其它類的實例句柄屬性。
if (_methodTest)
{
[_handle performSelector:_methodTest withObject:nil];//這裏面原來self屬爲相應的實例句柄
}
5、使用:
兩個對象之間的鬆散的耦合的時候的相互調用;
6、特別情況:
SEL 查找的方法不支持類方法
7、參數問題:
NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(switchLight:) userInfo:nil repeats:YES];
若是想帶上參數,則在 userInfo中設置;
如:
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:tableView forKey:@"table"];
[myDictionary setObject:indexPath forKey:@"indexPath"];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer:) userInfo:myDictionary repeats:NO];
}
- (void)onTimer:(NSTimer *)timer {
NSLog(@"--- %@", [timer userInfo] );
}