开发者

adding uibutton to a detail view programmatically

how is it possible to add a uibutton that can开发者_如何学Python display a phone number and call that number automactically loading data from an array for each table view cell


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


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]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜