繁体   English   中英

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

[英]adding uibutton to a detail view programmatically

如何添加一个可以显示电话号码的uibutton并呼叫该号码,以自动方式从每个表视图单元的数组中加载数据

苹果文档告诉您使用tel:// url方案。 而这个线程

举一个很好的例子:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM