Reputation: 77
In my app, I have created a button. On the click of that button, there is a long task that is performed and the button is disabled when the task is started and enabled back when the task is complete. My problem is that even after the UIbutton is disabled, it registers the click event and calls the same method after the task is complete.
I have attached my code below:
- (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;
}
Upvotes: 1
Views: 1379
Reputation: 2177
If your button sends messages to the target on UIControlEventTouchUpInside, then you can still touch down on the button before it gets disabled and touch up after it got disabled and it will still send a message to its target.
Try using this to stop receiving touch events:
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
Obviously you are going to want to add the target again when you reenable the button.
Upvotes: 2
Reputation: 77
Thanks for suggestions. It works fine when I added delay for enable back:
[self performSelector:@selector(enableNow) withObject:nil afterDelay:1.0];
Upvotes: 0
Reputation: 122458
You must be disabling it incorrectly. The docs for UIButton.enabled
state:
If the enabled state is NO, the control ignores touch events and subclasses may draw differently.
Check that DisableButton
is being called by adding an NSLog()
call, or use GCD and do away with the DisableButton
method altogether:
dispatch_sync(dispatch_get_main_queue(), ^{
self.view.userInteractionEnabled = NO;
_btn_sync_outlet.enabled = NO;
_btn_sync_outlet.userInteractionEnabled = NO;
});
Upvotes: 2