【问题标题】:click on a label in Swift单击 Swift 中的标签
【发布时间】:2015-10-01 06:20:17
【问题描述】:

我想制作点击它以拨打电话的标签。我知道 iOS 有这个选项,但是我在 Swift 中怎么做呢?

我在 ObjC 中找到了方法:

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

谁能帮帮我?

【问题讨论】:

标签: ios objective-c iphone swift call


【解决方案1】:
UIApplication.sharedApplication().openURL(NSURL(string: "tel://2135554321"))

示例

if let  CallURL:NSURL = NSURL(string:"tel://\(yourMobileNUmber)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL( CallURL)) {
  application.openURL( CallURL);
  }
else
 {
   // your number not valid
   let tapAlert = UIAlertController(title: "Alert!!!", message: "Your mobile number is invalid", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
 self.presentViewController(tapAlert, animated: true, completion: nil)
  }
 }

Type-2

  // add gesture to your Label
 var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
 yourLabelName.userInteractionEnabled=true
 yourLabelName.addGestureRecognizer(tapGesture)

// handle the function of UILabel
func handleTap(sender:UITapGestureRecognizer){
     if let  CallURL:NSURL = NSURL(string:"tel://\(yourMobileNUmber)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL( CallURL)) {
  application.openURL( CallURL);
  }
 else
 {
   // your number not valid
   let tapAlert = UIAlertController(title: "Alert!!!", message: "Your mobile number is invalid", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
 self.presentViewController(tapAlert, animated: true, completion: nil)
  }
 }
}

【讨论】:

  • 我可以把它连接到标签上吗?标签没有点击功能。那么,我该如何实现呢?
  • 检查更新的答案兄弟..,在标签的地方使用自定义UIButton,很容易工作
  • 如果你需要帮助可以问我,我希望和你一起\
  • 我更喜欢第二个版本,但它不起作用=/
  • 你遇到了什么错误,放置断点并检查方法是否被调用,否则请部分更新你的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 2021-09-25
  • 1970-01-01
相关资源
最近更新 更多