Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swift iOS中编写合适的目标选择器?_Ios_Google Maps_Uibutton_Swift2_Xcode7 - Fatal编程技术网

如何在swift iOS中编写合适的目标选择器?

如何在swift iOS中编写合适的目标选择器?,ios,google-maps,uibutton,swift2,xcode7,Ios,Google Maps,Uibutton,Swift2,Xcode7,所以我用这个按钮来给谷歌地图指明方向,问题是自从最新的swift以来,我不知道如何在UIButton中编写正确的目标选择器 这是我的按钮: directionButton.addTarget(self, action: #selector(ViewController.goToMap(String(format: "%.6f", place.coordinate.latitude), longitude: String(format: "%.6f", place.coordinate.longi

所以我用这个按钮来给谷歌地图指明方向,问题是自从最新的swift以来,我不知道如何在UIButton中编写正确的目标选择器

这是我的按钮:

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!)
        }

}

任何人都可以帮助我吗?

您不能使用
选择器传递参数,不能将方法修改为
goToMap()
,也不能将纬度和经度数据存储在实例变量中,以便在函数中使用。那你可以用

directionButton.addTarget(self, action: #selector(ViewController.goToMap()))

func goToMap(){
 // use value stored in instance var
 // self.latitude
 // self.longitude
}

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

directionButton.addTarget(self, action: #selector(ViewController.goToMap()))

func goToMap(){
 // use value stored in instance var
 // self.latitude
 // self.longitude
}