【问题标题】:Calling a phone number with Swift使用 Swift 拨打电话号码
【发布时间】:2015-07-10 19:45:17
【问题描述】:

如何将存储电话号码的变量分配给按钮以拨打该号码?

我正在使用 Swift。这是我的代码:

func base() {

    var myBase = sharedDefaults!.objectForKey("base") as? String

    if myBase != nil {
        if myBase == "FRA" {
            titleLabel.text = "FRANKFURT"
            adressLabel.text = "some adress"
            var phone = "+49123456 69 69804616"
            coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
            logoImage.image = UIImage(named: "flaggermany")

        } else if myBase == "LHR" {
            titleLabel.text = "LONDON"
            adressLabel.text = "some adress"
            var phone = "+44123456"

            coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)

            logoImage.image = UIImage(named: "flaguk")

        }
    }

    @IBAction func phoneButtonPressed(sender: AnyObject) {
        // let telUrlString = "tel://" + phone
        // UIApplication.sharedApplication().openURL(NSURL(string: "\      (telUrlString)")!)
    }

【问题讨论】:

    标签: ios swift button phone-number phone-call


    【解决方案1】:

    您可以将 phone 变量声明为您的类的全局变量,方法是在类声明的下方声明它,如下所示:

    class ViewController: UIViewController {
    
         let sharedDefaults = NSUserDefaults.standardUserDefaults()
         var phone = ""
    
    }
    

    通过声明这种方式,您可以在类中的任何位置访问它,现在您可以在函数中以这种方式为其赋值:

    func base() {
    
        var myBase = sharedDefaults.objectForKey("base") as? String
    
        if myBase != nil {
            if myBase == "FRA" {
    
                titleLabel.text = "FRANKFURT"
                adressLabel.text = "some adress"
                phone = "+49123456 69 69804616"
                coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
                logoImage.image = UIImage(named: "flaggermany")
    
    
            }
            else if myBase == "LHR" {
                titleLabel.text = "LONDON"
                adressLabel.text = "some adress"
                phone = "+44123456"
                coorPhone.setTitle("Duty Desk : \(phone)", forState: .Normal)
                logoImage.image = UIImage(named: "flaguk")
    
            }
        }
    }
    

    之后你就可以用这种方式把它用到另一个函数中了:

    @IBAction func phoneButtonPressed(sender: AnyObject) {
    
        if let url = NSURL(string: "tel://\(phone)") {
            UIApplication.sharedApplication().openURL(url)
        }
    }
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案2】:
      @IBAction func phoneButtonPressed(sender: UIButton) {
          if let url = NSURL(string: "tel://\(phone)") {
              UIApplication.sharedApplication().openURL(url)
          }
      }
      

      如果您在 viewController.swift 文件的顶部(在类调用下方)定义变量 phone,则可以使用此代码,这样就可以在函数中访问它。

      【讨论】:

        猜你喜欢
        • 2019-05-27
        • 1970-01-01
        • 2018-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-11
        • 2015-01-31
        • 1970-01-01
        相关资源
        最近更新 更多