我在表格中有四个单元格(UITableView),第一个和第二个单元格将我带到一个" ViewController"以下代码对我来说非常适合。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let segueIdentifier: String
switch indexPath.row {
case 0: //for first cell
segueIdentifier = "ubicacion"
case 1: //for second cell
segueIdentifier = "companias"
case 3: // For third cell
// Open un link using SFSafariViewController
default: //For fourth cell
// call phone
}
self.performSegue(withIdentifier: segueIdentifier, sender: self)
}
我的问题是关于第三和第四格,我该如何发送一个动作?
第三个单元格:您必须使用" SFSafariViewController"
打开链接第四种:当您点击时,您必须拨打指定的号码。
如果你可以指导我,我将不胜感激
答案 0 :(得分:1)
要在Safari中打开链接,请使用
if let url = URL(string: "YOUR URL") {
UIApplication.shared.openURL(url)
}
要拨打电话号码,请使用
if let url = NSURL(string: "tel://\(PHONE NUMBER)"), UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.shared.openURL(url)
}
注意:强>
你应该只使用performSegue
作为案例0&另外,我认为您的案例3实际上是案例2.您可以将代码更新为
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0: //for first cell
performSegue(withIdentifier: "ubicacion", sender: self)
case 1: //for second cell
performSegue(withIdentifier: "companias", sender: self)
case 2: // For third cell
if let url = URL(string: "YOUR URL") {
UIApplication.shared.openURL(url)
}
default: //For fourth cell
if let url = NSURL(string: "tel://\(PHONE NUMBER)"), UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}
}
答案 1 :(得分:0)
我在swift 4中的最终代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0: //for first cell
performSegue(withIdentifier: "ubicacion", sender: self)
case 1: //for second cell
performSegue(withIdentifier: "companias", sender: self)
case 2: // For third cell
let urlGruasWeb = URL(string: "https://www.google.com/")
let vistaGruas = SFSafariViewController(url: urlGruasWeb!)
present(vistaGruas, animated: true, completion: nil)
vistaGruas.delegate = self as? SFSafariViewControllerDelegate
default: //For fourth cell
let url: NSURL = URL(string: "tel://\(911)")! as NSURL
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}