是否可以为 UILabel 提供电话呼叫操作?

adr*_*nry 3 phone-call uibutton uilabel ios parse-platform

我正在我的应用程序中以编程方式从 Parse.com 获取字符串到 UILabel 中。该字符串是电话号码。所以我的问题是,是否可以在单击时对 UILabel 进行操作以进行调用。或者我是否必须从按钮中获取数据,或者更好的是,这可能吗?

如果可以的话我该怎么办?有人有我可以遵循的示例或教程吗?任何帮助,将不胜感激。谢谢!

Mic*_*edo 5

你的问题分为两部分:

  1. 粘贴 UILabel 时执行任何类型的操作
  2. 执行电话呼叫操作(第一部分中使用的操作)

首先,要在粘贴标签时执行操作,您必须向该标签添加点击手势识别器:

phoneNumberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = 
   [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneNumberLabelTap)];
[phoneNumberLabel addGestureRecognizer:tapGesture];
Run Code Online (Sandbox Code Playgroud)

然后你必须实现你的phoneNumberLabelTap方法:

 -(void)phoneNumberLabelTap
{
    NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",phoneNumberLabel.text]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    } else {
        UIAlertView * calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [calert show];
    }
}
Run Code Online (Sandbox Code Playgroud)