我在我的应用程序中以编程方式从Parse.com获取字符串到UILabel。 String是一个电话号码。所以我的问题是,是否可以通过点击拨打电话给UILabel做一个动作..或者我是否必须从按钮获取数据,或者更好的是,这可能吗?
如果有可能,我该怎么办?有没有人有我可以遵循的例子或教程? 任何帮助,将不胜感激。 谢谢!
答案 0 :(得分:4)
你的问题分为两部分:
首先,要在贴标签时执行操作,您必须在此标签上添加点击手势识别器:
phoneNumberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneNumberLabelTap)];
[phoneNumberLabel addGestureRecognizer:tapGesture];
然后你必须实现你的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];
}
}
答案 1 :(得分:1)
mobileLabel.isUserInteractionEnabled = true;
let tap = UITapGestureRecognizer(target: self, action:#selector(self.phoneNumberLabelTap))
tap.delegate = self
mobileLabel.addGestureRecognizer(tap)
func phoneNumberLabelTap()
{
let phoneUrl = URL(string: "telprompt:\(mobileLabel.text ?? "")")!
if(UIApplication.shared.canOpenURL(phoneUrl)) {
UIApplication.shared.openURL(phoneUrl)
}
else {
Constants.ShowAlertView(title: "Alert", message: "Cannot place call", viewController: self, isFailed: false)
}
}