验证码倒计时

属性:

UIButton *rightButton;//获取验证码

NSTimer *_timer;

NSInteger time;


初始化时间 :time=60;


功能:

#pragma mark =======获取验证码

- (void)codeClick:(UIButton *)button{

[self.view endEditing:YES];

BOOL isPhone = [Ht_control isMobileNumber:mobileTextField.text];

if (!isPhone) {

[Ht_control showMessage:@"请输入有效手机号"];

return;

}

[self creatCodeData];

}

#pragma mark ====获取验证码

- (void)creatCodeData{

rightButton.enabled = NO;

[rightButton setTitle:[NSString stringWithFormat:@"获取验证码(%zi)", time] forState:UIControlStateNormal];

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];

//请求数据


}

- (void)timeDown

{

time --;

if (time == 0) {

[rightButton setTitle:@"重新获取" forState:UIControlStateNormal];

rightButton.enabled = YES;

[_timer invalidate];

_timer = nil;

time = 60;

return;

}

[rightButton setTitle:[NSString stringWithFormat:@"获取验证码(%zi)", time] forState:UIControlStateNormal];

}

你可能感兴趣的:(验证码倒计时)