验证码 NSTimer 60秒刷新 ,禁用button
//
// ViewController.m
// AutoCode-01
//
// Created by Apple on 15/9/4.
// Copyright (c) 2015年 Sayimo. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
NSInteger secondsCountDown;
NSTimer * countDownTimer;
__weak IBOutlet UIButton *autoCodeButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
secondsCountDown=60;
}
- (IBAction)autoCode:(UIButton *)sender {
secondsCountDown=60;
[autoCodeButton setTitle:[NSString stringWithFormat:@"%zd",secondsCountDown] forState:UIControlStateNormal];
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
autoCodeButton.enabled=NO;
}
-(void)timeFireMethod{
secondsCountDown--;
[autoCodeButton setTitle:[NSString stringWithFormat:@"%zd",secondsCountDown] forState:UIControlStateNormal];
if (secondsCountDown<0) {
[countDownTimer invalidate];
autoCodeButton.enabled=YES;
[autoCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
}
}
@end