gpt4 book ai didi

ios - 如何阻止禁用的 UIbutton 执行点击操作

转载 作者:行者123 更新时间:2023-11-28 20:08:12 26 4
gpt4 key购买 nike

在我的应用程序中,我创建了一个按钮。单击该按钮时,会执行一个较长的任务,该按钮在任务开始时禁用,并在任务完成后重新启用。我的问题是,即使在 UIbutton 被禁用后,它也会注册点击事件并在任务完成后调用相同的方法。

我在下面附上了我的代码:

- (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;
}

最佳答案

您一定是错误地禁用了它。 docs UIButton.enabled 状态:

If the enabled state is NO, the control ignores touch events and subclasses may draw differently.

通过添加 NSLog() 调用检查 DisableButton 是否被调用,或者使用 GCD 并完全取消 DisableButton 方法:

dispatch_sync(dispatch_get_main_queue(), ^{
self.view.userInteractionEnabled = NO;
_btn_sync_outlet.enabled = NO;
_btn_sync_outlet.userInteractionEnabled = NO;
});

关于ios - 如何阻止禁用的 UIbutton 执行点击操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21283286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com