iOS 按钮连续点击 最后才一次提交
2018-11-12 本文已影响21人
赵哥窟
@interface ViewController ()
@property (nonatomic, strong) NSTimer *timer;//定时器
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"连续点击按钮" forState:UIControlStateNormal];
[self.view addSubview:btn];
btn.frame=CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor grayColor];
[btn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
}
- (void)Click:(UIButton *)btn {
[self.timer invalidate];
self.timer = nil;
self.timer =[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(requestData) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)requestData{
NSLog(@"我请求数据啦");
}