Alex
Alex

Reputation: 21

adding uibutton to a detail view programmatically

how is it possible to add a uibutton that can display a phone number and call that number automactically loading data from an array for each table view cell

Upvotes: 0

Views: 490

Answers (2)

joelm
joelm

Reputation: 8771

The Apple docs tell you to use the tel:// url scheme. And this thread

gives a good example:

NSString *phoneStr = [NSString stringWithFormat:@"tel:%@",[self.contactDetails objectForKey:@"phone"]];
NSString *escaped = [phoneStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];

Upvotes: 1

jrtc27
jrtc27

Reputation: 8526

Place a button in each cell, and set the text to the phone number from the array. Then, on the button's press selector, invoke the url tel:<PHONE NUMBER>:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    UIButton *callButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [callButton addTarget:self selector:@selector(callButtonPressed:)];
    [callButton setTitle:@"<PHONE NUMBER>"];
    [cell addSubview:callButton];
}

- (void)callButtonPressed:(id)sender {
    NSString *phoneURLAsString = [NSString stringWithFormat:@"tel:%@", sender.currentTitle];
    NSString *escapedPhoneURLAsString = [phoneURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:escapedPhoneURLAsString]];
}

Upvotes: 1

Related Questions