博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS SEL的理解与使用
阅读量:7066 次
发布时间:2019-06-28

本文共 2199 字,大约阅读时间需要 7 分钟。

   有很多人,认为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"
);
}

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

转载地址:http://sksll.baihongyu.com/

你可能感兴趣的文章
DHCP知识点
查看>>
Stimulsoft Reports.Net基础教程(四):创建主从报表②
查看>>
ubuntu14.04安装nginx
查看>>
2014年spark开发者大赛火热进行中!
查看>>
nginx负载均衡配置
查看>>
黑马程序员--WPF学习笔记二-----WPF常用页面布局
查看>>
微信公众号支付PHP-SDK的一个BUG
查看>>
网络 基于TCP协议socket编程
查看>>
现代软件工程讲义 目录
查看>>
Akka2使用探索6(Futures)——实现并发和异步
查看>>
【持续更新】jQuery 实用技巧
查看>>
大象也能起舞,Citrix X1计划让你对笔记本电脑say good bye
查看>>
Nginx 之常见报错问题解决
查看>>
linux 防爆破方法
查看>>
Elasticsearch的使用场景深入详解
查看>>
day10---paramiko ssh ftp
查看>>
2、通过ipmitool工具修改IPMI的WEB密码
查看>>
云盘关闭,教你用蒲公英搭建私有云
查看>>
Spring Cloud 入门教程5、服务容错监控:Hystrix Dashboard
查看>>
很好的学习平台
查看>>