我有一个带有2个组件(列)的UIPickerView,当用户完成选择时需要将其解除。我希望它在2秒内等待用户消失。在用户执行任何选择的那2秒内,它将继续等待另外2秒。这是我的代码,让它在解雇之前等待2秒。谢谢。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
double delayInSeconds = 2; // number of seconds to wait
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.view endEditing:YES];
});
}
答案 0 :(得分:2)
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(todo) userInfo:nil repeats:NO];
-(void) todo {
[self.view endEditing:YES];
}
停止此
[timer invalidate];
答案 1 :(得分:0)
我在pickerView: titleForRow: forComponent:
中使用简单的技巧找到了一个解决方案,并提供了@XEDeveloper和@rmaddy提供的有用建议。这是我的代码:
NSTimer *timer;
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(dismiss) userInfo:nil repeats:NO];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
[timer invalidate];
if (component==0) {
return [pickerFromArray objectAtIndex:row];
} else {
return [pickerToArray objectAtIndex:row];
}
}