在开发过程中有获取验证码时,显示等待时间。
废话不多说,直接上代码吧。
我是在Xib中创建的按钮,具体代码如下:
#import "ViewController.h"
@interface ViewController ()
{
int wait120S;
}
@property (weak, nonatomic) IBOutlet UIButton *showWaitBtn;
- (IBAction)showwaitTime:(id)sender;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//开始90S计时
- (void)startCount{
wait120S--;
NSString *titleStr = [NSString stringWithFormat:@"%d", wait120S];
[_showWaitBtn setTitle:[NSString stringWithFormat:@"%@s",titleStr] forState:UIControlStateNormal];
if(wait120S <= 0){
[_showWaitBtn setTitle:@"重获" forState:UIControlStateNormal];
_showWaitBtn.userInteractionEnabled = YES;
}else{
[self performSelector:@selector(startCount) withObject:nil afterDelay:1];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showwaitTime:(id)sender {
wait120S = 120;
_showWaitBtn.userInteractionEnabled = NO;
[self startCount];
}
代码完毕,单纯的看代码,恩,没毛病?。但是运行的时候,卧槽,发生了很诡异的情况,正常情况是只有数字闪一闪就可以了,但是那几个字(s重新获取)也跟着闪了。仔细检查了好几遍代码,感觉没问题啊。真的很蛋疼啊。哎,等一下,这个xib是不是有问题呢。打开xib,我擦,按钮的类型默认是系统类型的,这不是我想要的啊,果断切到custom类型,运行了下。恩,完美??