前言
我们做登录的时候经常会使用到,验证手机号是否正确、向手机发送验证码倒计时60s的问题,我们改如何解决呢?让我们一起来探讨一下吧。
如下图:
首先,我们先说说判断手机号码是否正确的问题吧,我的想法是给字符串添加一个分类,然后写上这样的代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
+ (bool)valimobile:(nsstring *)mobile{
if (mobile.length != 11){
//判断手机号码是否为11位
return no;
}else{
//使用正则表达式的方法来判断手机号
/**
* 移动号段正则表达式
*/
nsstring *cm_num = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\\\d{8}|(1705)\\\\d{7}$";
/**
* 联通号段正则表达式
*/
nsstring *cu_num = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\\\d{8}|(1709)\\\\d{7}$";
/**
* 电信号段正则表达式
*/
nsstring *ct_num = @"^((133)|(153)|(177)|(18[0,1,9]))\\\\d{8}$";
//初始化nspredicate对象
nspredicate *pred1 = [nspredicate predicatewithformat:@"self matches %@", cm_num];
//与具体对象进行筛选判断, 返回为bool值
bool ismatch1 = [pred1 evaluatewithobject:mobile];
nspredicate *pred2 = [nspredicate predicatewithformat:@"self matches %@", cu_num];
bool ismatch2 = [pred2 evaluatewithobject:mobile];
nspredicate *pred3 = [nspredicate predicatewithformat:@"self matches %@", ct_num];
bool ismatch3 = [pred3 evaluatewithobject:mobile];
if (ismatch1 || ismatch2 || ismatch3) {
return yes;
}else{
return no;
}
}
}
|
如果大家对于nspredicate的用法有些疑问的话可以看看这篇文章:http://www.zzvips.com/article/164587.html
1、我给button创建了一个分类
2、设定button上的文字,并记录倒计时的总时长,然后开一个定时器,并且关闭button的点击事件
3、定时器中将总时间缩减,并且设置button的文字,然后做一个判断,判断时间是否归为0,如果为0 就释放定时器,然后设置button上的文字,然后打开用户交互。
代码如下:
.h文件中
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#import@interface uibutton (btntime)
/**
按钮倒计时的问题
@param countdowntime 倒计时的时间(分钟)
*/
- (void)buttonwithtime:(cgfloat)countdowntime;
@end
|
.m文件中
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#import "uibutton+btntime.h"
/** 倒计时的显示时间 */
static nsinteger secondscountdown;
/** 记录总共的时间 */
static nsinteger alltime;
@implementation uibutton (btntime)
- (void)buttonwithtime:(cgfloat)countdowntime {
self.userinteractionenabled = no;
secondscountdown = 60 * countdowntime;
alltime = 60 * countdowntime;
[self settitle:[nsstring stringwithformat:@"%lds后重新获取",secondscountdown] forstate:uicontrolstatenormal];
[nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(timefiremethod:) userinfo:nil repeats:yes];
}
-(void)timefiremethod:(nstimer *)countdowntimer{
//倒计时-1
secondscountdown--;
//修改倒计时标签现实内容
[self settitle:[nsstring stringwithformat:@"%lds后重新获取",secondscountdown] forstate:uicontrolstatenormal];
//当倒计时到0时,做需要的操作,比如验证码过期不能提交
if(secondscountdown == 0){
[countdowntimer invalidate];
[self settitle:@"重新获取" forstate:uicontrolstatenormal];
secondscountdown = alltime;
self.userinteractionenabled = yes;
}
}
@end
|
代码已经上传到github上去了,地址:https://github.com/zhangyqyx/countdown
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 72
-
2025-05-26 71
-
2025-05-25 80
-
2025-05-26 50
-
2025-05-29 45
热门评论


