在我的应用中,我创建了一个按钮。 单击该按钮时,执行一项长任务,并在任务启动时禁用该按钮,并在任务完成时启用该按钮。 我的问题是,即使禁用了UI按钮,它也会注册click事件,并在任务完成后调用相同的方法。
我在下面附上了我的代码:
- (IBAction)sync_data_all:(id)sender {
// disable button on main thread
[NSThread detachNewThreadSelector:@selector(DisableButton) toTarget:self withObject:nil];
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
// long tasks start
[self fetchUNsyncedOrders];
[self DeleteproductCategoryTable];
[self deleteproductTable];
[self deletecustomersGroupsTable];
[self deletefetchCustomersTable];
[self deletefetchCustomersAddress];
[self deletefetchOrders];
[self deletefetchOrderItems];
[self deleteCreateOrdersGroups];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
[NSThread detachNewThreadSelector:@selector(StartActivityIndicatorIn) toTarget:self withObject:nil];
progress = 0.0f;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
@try {
[self productCategoryTable];
progress = 0.3;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
[self productTable];
progress = 0.4;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
[self customersGroupsTable];
progress = 0.5;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
[self fetchCustomersTable];
progress = 0.6;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
[self fetchCustomersAddress];
progress = 0.7;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
[self fetchOrders];
progress = 0.8;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
[self fetchOrderItems];
progress = 1.0;
[NSThread detachNewThreadSelector:@selector(progressBarProgress) toTarget:self withObject:nil];
[self CreateOrdersGroups];
}
@catch (NSException *exception)
{
[NSThread detachNewThreadSelector:@selector(syncNotComplete) toTarget:self withObject:nil];
syncError = YES;
}
@finally {
[self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.0];
}
// long task stop
[indicator stopAnimating];
self.view.userInteractionEnabled = YES;
_btn_sync_outlet.enabled = YES;
_btn_sync_outlet.userInteractionEnabled = YES;
}
- (void)DisableButton {
self.view.userInteractionEnabled = NO;
_btn_sync_outlet.enabled = NO;
_btn_sync_outlet.userInteractionEnabled = NO;
}
答案 0 :(得分:2)
您必须正确禁用它。 UIButton.enabled
状态的docs:
如果启用状态为NO,则控件忽略触摸事件和 子类可能会有不同的描述。
通过添加DisableButton
电话检查是否正在调用NSLog()
,或使用GCD并完全取消DisableButton
方法:
dispatch_sync(dispatch_get_main_queue(), ^{
self.view.userInteractionEnabled = NO;
_btn_sync_outlet.enabled = NO;
_btn_sync_outlet.userInteractionEnabled = NO;
});
答案 1 :(得分:1)
如果您的按钮在UIControlEventTouchUpInside上向目标发送消息,那么您仍然可以在按钮被禁用之前触摸它,并在禁用后触摸它并且它仍然会向其目标发送消息。
尝试使用此功能停止接收触摸事件:
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
显然,当您重新启用按钮时,您将要再次添加目标。
答案 2 :(得分:0)
感谢您的建议。当我添加延迟启用后,它工作正常:
[self performSelector:@selector(enableNow) withObject:nil afterDelay:1.0];