如何通过一个按钮拨打不同的电话号码?

时间:2014-07-06 23:22:16

标签: ios

我试图按下一个按钮,按下后会调用UIView上显示的人。每个人都通过segue传递他们的信息,只在故事板中使用模板UIWebView,但当点击列表中的某个人并显示他们的信息时,他们将显示为另一个人。它适用于图像,标题和标签,但不适用于调用短信或分页的按钮,因为它表示不兼容的指针类型,那么我将如何解决此问题呢?另外,我怎么能让短信功能能够在这段代码中工作?调用和分页在尝试创建调用时工作正常,但由于第一个问题而无效。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = self.person.personName;
    self.personImageView.image = [UIImage imageNamed:self.person.personImage];
    self.nameLabel.text = self.person.personName;
    self.textButton = self.person.textNumber; //Warning here: incompatible pointer type from NSString to UIButton"
    self.callButton = self.person.phoneNumber; //Warning here: incompatible pointer type from NSString to UIButton?
    self.pageButton = self.person.pagingNumber; //Warning here: incompatible pointer type from NSString to UIButton

}

-(IBAction)callPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://phoneNumber"]];
}

-(IBAction)textPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"smsprompt://textNumber"]];
}

-(IBAction)pagePhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://pagingNumber"]];
}

3 个答案:

答案 0 :(得分:0)

您正在尝试将NSString分配给UIButton

  1. self.callButton是(UIButton *)
  2. self.person.phoneNumber是(NSString *)
  3. 要设置按钮的名称,请执行以下操作:

    [self.callButton setTitle:self.person.phoneNumber
                     forState:UIControlStateNormal
    ];
    [self.callButton addTarget:self
                        action:@selector(callPhone:)
              forControlEvents:UIControlEventTouchUpInside
    ];
    

答案 1 :(得分:0)

因为您的按钮是每个“用户卡”的通用布局,我建议您通过segue而不是按钮本身传递相对于按钮的信息:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = self.person.personName;
    self.personImageView.image = [UIImage imageNamed:self.person.personImage];
    self.nameLabel.text = self.person.personName;
    self.numberValue = self.person.textNumber; 
    self.phoneNumberValue = self.person.phoneNumber; 
    self.pagingValue = self.person.pagingNumber; 

}

-(IBAction)callPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@", self.phoneNumberValue]];
}

-(IBAction)textPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"smsprompt:%@", self.numberValue]]];
}

-(IBAction)pagePhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@", self.pagingValue]];
}

self.numberValueself.phoneNumberValueself.pagingValueNSString

编辑:传递值的简单示例

在下面的示例中,我将向您展示如何通过控制器传递您的值,请注意我没有关注您的“布局”,而是通过包含我的推荐(传递数据而不是按钮本身)来关注您的问题。我也自愿删除了此示例的非相关方法(didReceiveMemoryWarninginitWithNibName等。)。

希望它会有所帮助

UITableViewController.m:

@implementation _4601253ViewController {
    UITableView *tableView;
    NSMutableArray *phoneNumberList;
}

- (void)viewDidLoad
{
    /* Init objects */
    phoneNumberList = [[NSMutableArray alloc] initWithObjects:@"232323", @"434445", @"367843", @"938472", nil];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    tableView.delegate = self;
    tableView.dataSource = self;

    [self.view addSubview:tableView];
}

#pragma mark - UITableView DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [phoneNumberList count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.backgroundColor = [UIColor clearColor];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 30, 20)];
    label.text = @"phone number:";

    UILabel *phoneNumberValue = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 100, 20)];
    phoneNumberValue.text = [NSString stringWithFormat:@"+262692 %@", [phoneNumberList objectAtIndex:indexPath.row]];
    phoneNumberValue.font = [UIFont fontWithName:@"Arial" size:10];

    [cell addSubview:phoneNumberValue];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReceiverViewController *rvc = [[ReceiverViewController alloc] init];
    rvc.phoneNumberValue = [phoneNumberList objectAtIndex:indexPath.row];
    [self presentViewController:rvc animated:YES completion:nil];
    //[self.navigationController pushViewController:rvc animated:YES];
}

您收到UIViewController.m:

@implementation ReceiverViewController

@synthesize phoneNumberValue = _phoneNumberValue;    // In your .h you should define this property

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor lightGrayColor];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 100, 250, 30)];
    [button.layer setBorderColor:[UIColor blackColor].CGColor];
    [button.layer setBorderWidth:1.0];
    [button setTitle:[NSString stringWithFormat:@"call me: +262692 %@", _phoneNumberValue] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];
}

- (void)buttonTapped:(id)sender
{
    // Your method here
}

答案 2 :(得分:0)

我实际上能够通过玩你们给我的代码来完成这项工作,并最终得到了这个。

[self.callButton addTarget:self action:@selector(callPhone:) forControlEvents:UIControlEventTouchUpInside];//call button

-(IBAction)callPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@", self.person.phoneNumber]]];

非常感谢帮助解决这个问题。虽然短信按钮仍然无法工作,任何有关修复的建议都会受到高度赞赏。