当我触摸tableview内的按钮时,它自动转到TabGesture方法,但我想要按钮触摸方法?

时间:2012-07-09 08:10:31

标签: iphone ios uitableview uibutton

目前我正在使用简单的iPhone应用程序,使用UITableview进入Emp_Name,Emp_Salary等字段,并使用UIButton设置像复选框一样的图像并设置目标和执行选择器, 当我触摸复选框按钮时,它会自动进入UITabGesture方法但不会进入按钮触摸方法。如何解决这个问题,请帮帮我

我尝试了源代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
 UITapGestureRecognizer  *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setNumberOfTouchesRequired:1];
    recognizer.cancelsTouchesInView = NO;
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release]; 
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer // TabGesture method
{
    NSLog(@"Tab received.");
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


 if (indexPath.section == 0 && indexPath.row == 0) 
    {


        perlessonCheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
        perlessonCheckButton.frame = CGRectMake(5, 5, 30, 30); 
        [perlessonCheckButton setImage:[UIImage imageNamed:@"tickAltered1.png"] forState:UIControlStateNormal];
        [perlessonCheckButton addTarget:self action:@selector(showPerLessonOptions) forControlEvents:UIControlEventTouchUpInside];
        [perlessonCheckButton setBackgroundColor:[UIColor lightTextColor]]; 
        [perlessonCheckButton setUserInteractionEnabled:NO];
        [cell.contentView addSubview:perlessonCheckButton];

    }
}



-(void) showPerLessonOptions // Button method
{
    [perlessonCheckButton setUserInteractionEnabled:NO];
    [flatFeeCheckButton setUserInteractionEnabled:YES];

    [perlessonCheckButton setImage:[UIImage imageNamed:@"tickAltered1.png"] forState:UIControlStateNormal];
    [flatFeeCheckButton setImage:[UIImage imageNamed:@"untickAltered1.png"] forState:UIControlStateNormal];
}

2 个答案:

答案 0 :(得分:0)

-(void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint tapLocation = [gestureRecognizer locationInView:self.view];

    if(CGRectContainsPoint(yourButton.frame, tapLocation))
    {
      [self showPerLessonOptions];
    }
}

答案 1 :(得分:0)

我认为问题在于这一行,

[perlessonCheckButton setUserInteractionEnabled:NO];

以上行禁用用户互动。 将其更改为,

[perlessonCheckButton setUserInteractionEnabled:YES];

如果上述方法无效,则可以在按钮上添加手势识别器。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPerLessonOptions:)];
[perlessonCheckButton  addGestureRecognizer:tap];
[perlessonCheckButton setUserInteractionEnabled:YES];

有关UITapGestureRecognizer

的更多信息