码迷,mamicode.com
首页 > 其他好文 > 详细

重新获取验证码的按钮

时间:2014-06-05 04:41:37      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:计时器   重新获取验证码   

很多应用在修改密码或者是更改个人信息时,需要用户输入手机验证码,其间有个等待过程,一般为60秒,等待结束后可以重新点击按钮获取新的验证码,在页面来回跳转之后又可以重新计时,简要做以下整理:

在.h文件中声明计时器

@interface LinViewController : UIViewController

@property (strong, nonatomic) UIButton * button;

@property (strong, nonatomic) NSTimer * timer;

@end

在.m中实现,特别是- (void)viewWillAppear:(BOOL)animated 和 - (void)viewWillDisappear:(BOOL)animated 方法

//当页面有跳转的操作的时间需要调用[self.timer invalidate],使计时器停止,否则会造成时间连续走动

static int myTime;

- (void)viewWillAppear:(BOOL)animated
{
    [self.timer invalidate];
    self.timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeButtonName) userInfo:nil repeats:YES];
    
    self.button.enabled = NO;
    self.button.titleLabel.text = @"10秒后重新获取";
    myTime = 10;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.timer invalidate];
}

- (void)changeButtonName
{
    if (myTime > 0) {
        self.button.enabled = NO;
        myTime--;
        NSString * string = [NSString stringWithFormat:@"%d秒后重新获取",myTime];
        NSLog(@"%@===",string);
        self.button.titleLabel.text = string;
    }else
    {
        [self.timer invalidate];
        self.button.enabled = YES;
    }
}

- (void)addButtonAction
{
    [self.timer invalidate];
    [self viewWillAppear:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.button setFrame:CGRectMake(90, 90, 200, 30)];
    [self.button setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
}


重新获取验证码的按钮,布布扣,bubuko.com

重新获取验证码的按钮

标签:计时器   重新获取验证码   

原文地址:http://blog.csdn.net/u012889435/article/details/27212061

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!