所以我有这个按钮来使用谷歌地图给出方向,问题是我不知道如何用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!)
}
}有人能帮我吗?
发布于 2016-06-08 00:26:25
您不能使用selector传递参数,请将您的方法修改为goToMap(),并将纬度和经度数据存储在实例变量的中,以便在函数中使用。然后你就可以使用
directionButton.addTarget(self, action: #selector(ViewController.goToMap()))
func goToMap(){
// use value stored in instance var
// self.latitude
// self.longitude
}https://stackoverflow.com/questions/37684375
复制相似问题