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
Asked
Active
Viewed 485 times
2 Answers
1
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]];
1
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]];
}

Nils Munch
- 8,805
- 11
- 51
- 103

jrtc27
- 8,496
- 3
- 36
- 68
-
thanks for this clear description however how would i add the numnbers to the array.. here is my code rootArray = [[NSArray alloc] initWithObjects:@"Athens", @"Thessalonik", @"Pluto", nil]; moonArray = [[NSArray alloc] initWithObjects:@"Ath 1", @"Ath2", @"Ptolemy", nil]; and what im trying to achieve is to add the button to a detailviewcontroller not to a table cell – Alex Aug 04 '10 at 18:16
-
so basically im asking how to add the phone numners to the array... thanks again – Alex Aug 04 '10 at 18:30
-
Can you clarify the structure you want please? (e.g. Having an array with lots of dictionaries etc.) – jrtc27 Aug 05 '10 at 12:49
-
a phone array that would load the phones into a uibutton displayed in a detailview controller. the uibutton needs to call the number according to the array. i have a separte array for phone numners and one for data is that ok? the template im using uses uilabel to dislay this information so im not sure how to implement this thanks – Alex Aug 06 '10 at 11:22