【问题标题】:How to write proper target selector in swift iOS?如何在 swift iOS 中编写正确的目标选择器?
【发布时间】:2023-04-04 03:39:01
【问题描述】:

所以我有这个按钮可以使用谷歌地图指示方向,问题是我不知道如何在 UIButton 中编写正确的目标选择器,因为最新的 swift

这是我的按钮:

directionButton.addTarget(self, action: #selector(ViewController.goToMap(String(format: "%.6f", place.coordinate.latitude), longitude: String(format: "%.6f", place.coordinate.longitude))), forControlEvents: UIControlEvents.TouchUpInside)

这是我的功能:

func goToMap(latitude: String, longitude: String) {
        print("function gotoMap works!!!!")
        if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
            UIApplication.sharedApplication().openURL(NSURL(string:
                "comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")!)
        } else {
            print("Can't use comgooglemaps://")

            var addressToLinkTo = ""

            addressToLinkTo = "http://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving"

            addressToLinkTo = addressToLinkTo.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

            let url = NSURL(string: addressToLinkTo)
            UIApplication.sharedApplication().openURL(url!)
        }

}

谁能帮帮我?

【问题讨论】:

    标签: ios google-maps uibutton swift2 xcode7


    【解决方案1】:

    您不能使用selector 传递参数,请将您的方法修改为goToMap() 并将纬度和经度数据存储在实例变量中以在函数中使用。那么你可以使用

    directionButton.addTarget(self, action: #selector(ViewController.goToMap()))
    
    func goToMap(){
     // use value stored in instance var
     // self.latitude
     // self.longitude
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多