Reputation: 411
I have a UITableView and I have taken phonenumber as one of the UILabel in UITableViewCell.When I click on that particular label then I should be able to make a call to that particular number.For UILabel to respond to clicks I took UITapGesture.But in detecting which number to be called I used [sender tag]which throws error:"[UITapGestureRecognizer tag]: unrecognized selector sent to instance"
- (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
}
How can I call to that particular number only which is clicked in tableviewcell? I want to confirm whether I will be able to test phonecalling from simulator ?
Upvotes: 0
Views: 162
Reputation: 9157
Your problem initially lies with this code:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)];
Where you initialize a UITapGestureRecognizer
and set its action to labelButton:
but because you do not specify a parameter and the method labelButton:
is asking for an id
argument, a tap gesture recognizer is getting passed into the labelButton
method instead of a UIButton
which is why it crashes because UITapGestureRecognizer
is not able to respond to tag
, it is not a UI object.
So to fix it its actually pretty easy, use this code:
-(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]]];
}
If this worked please upvote/tick!
Upvotes: 2