// 验证码 60秒倒计时
#define TIME_END_SECOND 60
定义全局变量:
NSInteger seconds;
-(void)setGetchecktitle
{
[self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
}
-(void)timerFireMethod:(NSTimer *)theTimer {
if (seconds == 0) {
[theTimer invalidate];
seconds = TIME_END_SECOND;
[getCodeBtn setTitle:@"获取验证码" forState: UIControlStateNormal];
[_getCodeBtn setEnabled:YES];
}else{
seconds--;
[theTimer timeInterval];
[self.getCodeBtn setEnabled:NO];
[self.getCodeBtn setTitle:timeStr forState:UIControlStateNormal];
NSString *timeStr = [NSString stringWithFormat:@"%d秒后重获取", seconds];
[self.getCodeBtn setTitle:timeStr forState:UIControlStateNormal];
}
}
//手机号码验证
-(BOOL)isValidatePhoneNumber:(NSString *)number
{
NSString *phoneRegex = @"^1[3|4|5|7|8][0-9]\\d{8}";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",phoneRegex];
BOOL isyes = [phoneTest evaluateWithObject:number];
return isyes;
}
提示:这里的button 设置为costom,要不然会闪烁
原文:http://my.oschina.net/sccx/blog/522183