IOS SEL的理解与使用

本文探讨了Objective-C中block与@selector的区别及应用。解释了@selector的作用和SEL类型的含义,并通过实例展示了如何使用@selector实现target-action模式,最后对比了block与代理模式的不同。

   有很多人,认为block的推广可取代代理设计模式,其实block并不能取代代理,代理的模式可以让代码逻辑性更强,更整洁,也会有更高的可读性和可扩展性。相比之下,我觉得block更多的是取代了选择器@selector。

   @selector是什么?我们要首先明白SEL,SEL并不是一种对象类型,我们通过xCode的字体颜色就可以判断出来,它是一个关键字,就像int,long一样,它声明了一种类型:类方法指针。其实就可以理解为一个函数指针。比如,我们生命一个叫myLog的函数指针:

?
1
2
3
4
5
6
7
#import "ViewController.h"
 
@interface ViewController ()
{
     SEL myLog;
}
@end

声明出了这个指针,我们该如何给它传递这个函数呢?有两种方式:

1、在编译时,使用@selector来取得函数

现在,我们应该明白@selector是什么了,它是一个编译标示,我们通过它来取到相应函数。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@interface ViewController ()
{
     SEL myLog;
}
@end
 
@implementation ViewController
 
- ( void )viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     myLog = @selector(myLogL);
     //通过performSelector来执行方法
    [self performSelector:myLog]; //打印 “myLog”
    
}
-( void )myLogL{
     NSLog(@ "myLog" );
}

2、在运行时,通过NSSelectorFromString方法来取到相应函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#import "ViewController.h"
 
@interface ViewController ()
{
     SEL myLog;
}
@end
 
@implementation ViewController
 
- ( void )viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     myLog = NSSelectorFromString(@ "myLogN" );
     [self performSelector:myLog];
    
}
 
 
-( void )myLogN{
     NSLog(@ "myLog" );
}

这两种方式的差别在于,编译时的方法如果没有找到相应函数,xcode会报错,而运行时的方法不会。

至于SEL的应用,我相信最广泛的便是target——action设计模式了。我们来简单模拟一下系统button的工作原理:

我们先创建一个继承于UIButton的类:

.h文件:

?
1
2
3
4
5
#import <UIKit/UIKit.h>
 
@interface Mybutton : UIButton
-( void )addMyTarget:(id)target action:(SEL)action;
@end

.m文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "Mybutton.h"
 
@implementation Mybutton
{
     SEL _action;
     id _target;
}
-( void )addMyTarget:(id)target action:(SEL)action{
     _target=target;
     _action=action;
}
-( void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
     [_target performSelector:_action];
}
 
 
@end

在外部:

?
1
2
3
4
5
6
7
8
9
10
11
- ( void )viewDidLoad {
     [super viewDidLoad];
     Mybutton * btn = [[Mybutton alloc]initWithFrame:CGRectMake(100, 100, 60, 60)];
     btn.backgroundColor=[UIColor redColor];
     [btn addMyTarget:self action:@selector(click)];
     [self.view addSubview:btn];
}
 
-( void )click{
     NSLog(@ "点击了btn" );
}

当然,如果要调用参数,系统提供的默认参数不超过两个,如果参数很多,一种是我们可以通过字典传参,另一种方法比较复杂,在这里先不讨论。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值