2

目前我正在使用简单的 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];
}
4

2 回答 2

0

我认为问题在于这条线,

[perlessonCheckButton setUserInteractionEnabled:NO];

上面的行禁用了用户交互。改成,

[perlessonCheckButton setUserInteractionEnabled:YES];

如果上述方法不起作用,那么您可以向按钮添加手势识别器。

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

有关UITapGestureRecognizer的更多信息

于 2012-07-11T12:38:49.527 回答
0
-(void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint tapLocation = [gestureRecognizer locationInView:self.view];

    if(CGRectContainsPoint(yourButton.frame, tapLocation))
    {
      [self showPerLessonOptions];
    }
}
于 2012-07-09T08:14:45.060 回答