从应用程序拨打电话号码

时间:2012-12-29 06:37:28

标签: iphone xcode

我有一个UITableView,我把phonenumber作为UITabel在UITableViewCell中的一个。当我点击那个特定的标签然后我应该能够调用那个特定的号码。为了UILabel响应点击我带UITapGesture 。但是在检测要调用的号码时我使用[发送者标签]会抛出错误:“ [UITapGestureRecognizer标签]:无法识别的选择器发送到实例”

- (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中单击的特定号码?我想确认一下我是否可以通过模拟器测试手机来电?

1 个答案:

答案 0 :(得分:2)

您的问题最初取决于此代码:

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

初始化UITapGestureRecognizer并将其操作设置为labelButton:但由于您未指定参数且方法labelButton:要求id参数,点击手势识别器已被传递到labelButton方法而不是UIButton这就是崩溃的原因,因为UITapGestureRecognizer无法响应tag,它不是用户界面对象

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

-(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]]];    
}

如果这有效,请upvote / tick!