这周的工作中遇到了实现点击按钮打电话的功能,为了能更加的熟练掌握,我就整理了一下打电话功能的,希望能在提高自己的同时,帮助到我们的初学者
`#import “ViewController.h”
@interface ViewController ()
@property(nonatomic,strong)NSString *str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.str=@”13333333333”;
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(50, 100, 100 ,80)];
button.backgroundColor=[UIColor redColor];
[button addTarget:self action:@selector(CallPhone) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
打电话按钮的的方法 点击button直接拨打电话
//-(void)CallPhone
//{
// //调用拨打电话按钮
NSString *tel= [NSString stringWithFormat:@”tel:%@”,self.phoneNum];
// NSString *tel=@”tel:15736709059”;
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
//}
//打电话的方法 带有提示框的打电话点击提示框的确定才能拨打电话
//-(void)CallPhone
//{
//
// if (self.str) {
// UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@”拨打电话” message:self.str delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil];
// [alertView show];
// }
//
//
//}
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
// if (alertView.cancelButtonIndex!=buttonIndex) {
// //调用拨打电话按钮
// NSString *tel= [NSString stringWithFormat:@”tel:%@”,self.str];
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
// }
//}
//iOS9中的新特新,带有提示框的拨打电话的方法
-(void)CallPhone
{
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@”打电话” message:self.str preferredStyle:UIAlertControllerStyleAlert];
//cancel这个Style是取消按钮
[alert addAction:[UIAlertAction actionWithTitle:@”取消” style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//点击按钮的响应事件
}]];
//
// UIAlertActionStyleDefault 自定义的按钮的点击方式
// UIAlertActionStyleCancel, 取消按钮的点击方式
// UIAlertActionStyleDestructive 有警告性质的按钮的点击方式 设置这个的时候按钮是红色的
//在style中Default是默认情况下的样式
[alert addAction:[UIAlertAction actionWithTitle:@”确定” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// //点击按钮的响应事件
NSString *tel=[NSString stringWithFormat:@”tel:%@”,self.str];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
}]];
//弹出提示框的方法
[self presentViewController:alert animated:true completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
`