【问题标题】:When i touch the button inside the tableview it automatically goes to TabGesture method, but i want button touch method?当我触摸 tableview 内的按钮时,它会自动转到 TabGesture 方法,但我想要按钮触摸方法?
【发布时间】:2012-07-09 08:10:31
【问题描述】:

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

【问题讨论】:

    标签: iphone ios uitableview uibutton


    【解决方案1】:
    -(void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
    {
        CGPoint tapLocation = [gestureRecognizer locationInView:self.view];
    
        if(CGRectContainsPoint(yourButton.frame, tapLocation))
        {
          [self showPerLessonOptions];
        }
    }
    

    【讨论】:

    • 是在点击按钮时调用 handleSingleTap 吗?如果是的话,它应该工作。我注意到您将其命名为 handleSwipeFrom,而我将其命名为 handleSingleTap。您可以将粘贴代码从我的方法复制到您的方法中。
    • -(void)handleSwipeFrom:(UIGestureRecognizer *)gestureRecognizer { NSLog(@"Tab received."); CGPoint tapLocation = [gestureRecognizer locationInView:self.view]; if(CGRectContainsPoint(perlessonCheckButton.frame, tapLocation)) { [self showPerLessonOptions]; }
    • 您接受了答案,它是否有效。我注意到 perlessonCheckButton 不是 self.view 的子视图,因此您需要调整框架。
    【解决方案2】:

    我认为问题在于这条线,

    [perlessonCheckButton setUserInteractionEnabled:NO];
    

    以上行禁用用户交互。 改成,

    [perlessonCheckButton setUserInteractionEnabled:YES];
    

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

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

    更多关于UITapGestureRecognizer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      相关资源
      最近更新 更多