// 1. 定义全局变量
NSInteger mTime;
NSTimer *mTimer;
viewDidLoad 中,设置mTime = 60(默认一分钟)
// 2. 后台请求验证码成功后,加定时器
[Utility toast:self.view message:@” 获取验证码成功”];
mTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeDel) userInfo:nil repeats:YES];
// 3. 写实现方法
-(void)timeDel{
if (mTime>1) {
mTime–;
sendBtn.titleLabel.text =[NSString stringWithFormat:@”%d秒后获取”,mTime ];
[sendBtn setTitle:[NSString stringWithFormat:@”%d秒后获取”,mTime ]forState:UIControlStateNormal]; //解决button闪动问题
sendBtn.userInteractionEnabled = NO;
}else{
[sendBtn setTitle:@”发送验证码” forState:UIControlStateNormal];
sendBtn.userInteractionEnabled=YES;
mTime=60;
[mTimer invalidate];
mTimer = nil;
sendBtn.backgroundColor = RGB(255.0, 107.0, 0.0, 1.0);
}
}