iOS学习篇章3--SEL与@selector


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];


在iOS提供的方法中,在@selector 中仅有一个参数是自带的NSTimer的参数,想要带上其他参数则不行;


若是想带上参数,则在 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] );
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值