以编程方式将uibutton添加到详细视图

时间:2010-08-04 10:29:55

标签: ios iphone

如何添加一个可显示电话号码的uibutton,并为每个表格视图单元格自动从数组中加载数据

2 个答案:

答案 0 :(得分:1)

Apple docs告诉您使用tel:// url方案。 这thread

给出了一个很好的例子:

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

答案 1 :(得分:1)

在每个单元格中放置一个按钮,并将文本设置为阵列中的电话号码。然后,在按钮的按下选择器上,调用网址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]];
}
相关问题