我正在我的应用程序中以编程方式从 Parse.com 获取字符串到 UILabel 中。 该字符串是电话号码。所以我的问题是,是否可以在单击时对 UILabel 进行操作以进行调用。或者我是否必须从按钮中获取数据,或者更好的是,这可能吗?
如果可以的话我该怎么办?有人有我可以遵循的示例或教程吗? 任何帮助,将不胜感激。 谢谢!
您的问题分为两部分:
首先,要在粘贴标签时执行操作,您必须向该标签添加点击手势识别器:
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];
}
}
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)
}
}
SWIFT 3+
为标签添加点击手势识别器
let tap = UITapGestureRecognizer(target: self, action:#selector(self.phoneNumberLabelTap))
supportCallLabel.isUserInteractionEnabled = true
supportCallLabel.addGestureRecognizer(tap)
}
phoneNumberLabelTap 方法
此处,Constants.supportCall 是您的电话号码
@objc func phoneNumberLabelTap() {
if let url = URL(string: "tel://\(Constants.supportCall)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}