簡體   English   中英

如何停止禁用的UIbutton執行點擊操作

[英]How to stop disabled UIbutton from performing click action

在我的應用程序中,我創建了一個按鈕。 單擊該按鈕時,將執行一個很長的任務,並且在啟動任務時禁用該按鈕,並在任務完成時將其啟用。 我的問題是,即使在禁用UIbutton之后,它也會注冊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;
}

您必須錯誤地禁用它。 UIButton.enabled狀態的文檔

如果啟用狀態為“否”,則控件將忽略觸摸事件,並且子類的繪制方式可能有所不同。

通過添加NSLog()調用來檢查是否正在調用DisableButton ,或者使用GCD並完全放棄DisableButton方法:

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

如果您的按鈕將消息發送到UIControlEventTouchUpInside上的目標,則您仍然可以在按鈕被禁用之前對其進行觸碰,並在按鈕被禁用之后進行觸動,並且仍將消息發送至目標。

嘗試使用此方法停止接收觸摸事件:

- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

顯然,當您重新啟用按鈕時,您將要再次添加目標。

感謝您的建議。 當我添加了啟用返回延遲時,它可以正常工作:

[self performSelector:@selector(enableNow) withObject:nil afterDelay:1.0];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM