4

我的按钮被禁用,但我想在用户点击它时显示一个消息框。我该怎么做?另外我想提一下 UIbutton 是在 UITableview 上添加的,我尝试了下面的代码,但它总是elseDisableClick()起作用。

cellForRowAtIndexPath:

button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(180, 6, 30, 30)];
    [button setTag:4000];
    [button addTarget:self action:@selector(click:event:) forControlEvents:UIControlEventTouchDown];
    UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DisableClick:)];
    [self.view addGestureRecognizer:gesture];
    [gesture release];
    [cell.contentView addSubview:button];

DisableClick()

- (void)DisableClick:(UIButton*)sender {
    UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender;
    CGPoint pt = [recognizer locationOfTouch:0 inView:market];
    if (CGRectContainsPoint(market.bounds, pt)) {
        NSLog(@"Disabled button tapped");

    }
    else 
    {
     NSLog(@"go out"); // it's always go to here
    }

}
4

3 回答 3

2

而不是禁用按钮保持启用。更改按钮的图像,如禁用图像。当点击按钮时放置一个标志变量使用这个标志变量来运行启用按钮和禁用按钮的代码

BOOL btnFlg;//initialize it true or false on cellForRowAtIndex... method
- (void)DisableClick:(UIButton*)sender 
{
   if(btnFlg)
   {
           btnFlag=NO;
           NSLog("button disabled");
   }
   else
   {
          btnFlag=YES;
          NSLog("button enabled");
   }
}
于 2013-10-04T04:31:22.173 回答
0

为什么要为此按钮初始化点击手势。您在您的addTarget中设置选择器

为按钮状态维护一个 btn 布尔值。每当您想要禁用或启用时更改状态。

BOOL btnEnabled;

 button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(180, 6, 30, 30)];
    [button setTag:4000];
    [button addTarget:self action:@selector(DisableClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

使目标函数为

- (void)DisableClick:(UIButton*)sender {

    //set the function that you needed when the user tap on this button here.
    if(btnEnabled)  {
     // here invoke the functions you needed when the button enabled
    } else {
       UIAlertView *msg = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Your button is disabled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
       [msg show];
    }

}
于 2013-10-04T04:36:17.873 回答
0

您将需要继承 UIButton 并实现 touchesBegan:withEvent: 和 touchesEnded:withEvent: 方法以满足您的目的。

于 2013-10-04T06:23:30.440 回答