0

我有一个 UITableView,并且我将 phonenumber 作为 UITableViewCell 中的 UILabel 之一。当我单击该特定标签时,我应该能够拨打该特定号码。对于 UILabel 响应点击,我使用了 UITapGesture。但在检测要呼叫的号码我使用了 [sender tag],它会引发错误:“ [UITapGestureRecognizer tag]:无法识别的选择器已发送到实例”

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  lblphone = [[UILabel alloc] initWithFrame:CGRectZero];
            lblphone.tag = 116;
            lblphone.backgroundColor = [UIColor clearColor];
            [lblphone setFont:[UIFont fontWithName:@"Helvetica" size:12]];
            [lblphone setLineBreakMode:UILineBreakModeWordWrap];
            [lblphone setUserInteractionEnabled:YES];
            UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)];
            [tapGestureRecognizer setNumberOfTapsRequired:1];
            [lblphone addGestureRecognizer:tapGestureRecognizer];
            [tapGestureRecognizer release];
            [cell addSubview:lblphone];


}

 CGSize constraint5 = CGSizeMake(320, 2000.0f);
                            CGSize size5=[phone sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
                            lblphone =(UILabel *)[cell viewWithTag:116];
                            [lblphone setFrame:CGRectMake(10,businessname.frame.size.height+businessname.frame.origin.y,320, size5.height)];
                            lblphone.textAlignment=UITextAlignmentLeft;
                            lblphone.backgroundColor=[UIColor clearColor];
                            lblphone.numberOfLines=0;
                            lblphone.lineBreakMode=NSLineBreakByClipping;
                            lblphone.font=[UIFont fontWithName:@"Helvetica" size:14];
                            lblphone.text=[NSString stringWithFormat:@"%@ ",phone ];
                            [lblphone sizeToFit];
}

-(IBAction)labelButton:(id)sender
{

   selectedrowCall=[sender tag]; //error at this line

   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://%@",[lblphone.text]]];//error at this line also :Expected Identifier

}

我如何才能呼叫仅在 tableviewcell 中单击的特定号码?我想确认我是否可以通过模拟器测试电话?

4

1 回答 1

2

您的问题最初在于此代码:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)];

在您初始化 aUITapGestureRecognizer并将其操作设置为labelButton:但因为您没有指定参数并且该方法labelButton:正在请求id参数的地方,轻击手势识别器被传递到该labelButton方法而不是 aUIButton这就是它崩溃的原因,因为UITapGestureRecognizer无法响应to tag,它不是 UI 对象。

因此,要修复它实际上非常简单,请使用以下代码:

-(IBAction)labelButton:(UITapGestureRecognizer *)sender
{
   selectedrowCall=[[sender view] tag]; // here we are referencing to sender's view which is the UILabel so it works!
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://%@",[lblphone text]]];    
}

如果这有效,请投票/打勾!

于 2012-12-29T06:58:15.883 回答