【问题标题】:How to stop disabled UIbutton from performing click action如何阻止禁用的 UIbutton 执行点击操作
【发布时间】:2014-02-12 12:46:06
【问题描述】:

在我的应用程序中,我创建了一个按钮。 单击该按钮时,将执行一个长任务,并且在任务启动时禁用该按钮,并在任务完成后重新启用该按钮。 我的问题是即使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;
}

【问题讨论】:

    标签: ios iphone objective-c ios7 uibutton


    【解决方案1】:

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

    如果启用状态为 NO,则控件将忽略触摸事件并 子类的绘制方式可能不同。

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

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

    【讨论】:

    • 感谢您评论我使用 UIButtonOulet.enabled=NO 正确禁用了 UIbutton,即使在我隐藏 UIbutton 它注册点击事件的情况下也是如此
    • @HARSHMAAN 尝试改用 GCD 代码(它会同步调用禁用代码)。
    • 感谢您的建议,当为启用返回添加延迟时它工作正常 [self performSelector:@selector(enableNow) withObject:nil afterDelay:1.0];
    【解决方案2】:

    感谢您的建议。当我为启用返回添加延迟时,它工作正常:

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

    【讨论】:

      【解决方案3】:

      如果您的按钮在 UIControlEventTouchUpInside 上向目标发送消息,那么您仍然可以在按钮被禁用之前触摸它并在它被禁用后触摸它仍然会向其目标发送消息。

      尝试使用它来停止接收触摸事件:

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

      很明显,当您重新启用按钮时,您会想要再次添加目标。

      【讨论】:

      • 哇,这是我的问题!太感谢了!你有这方面的资料吗?
      • 对不起,我的意思是苹果在任何地方都有记录吗?
      • 这种方法?不这么认为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多