2016-06-07 18 views
0

所以我有這個按鈕使用谷歌地圖給方向,問題是我不知道,因爲最新的迅速如何在swift iOS中編寫適當的目標選擇器?

這裏如何在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!) 
     } 

} 

任何人都可以幫助我嗎?

回答

3

您無法傳遞參數selector,修改您的方法爲goToMap()並將經緯度數據存儲在實例變量中以在該函數中使用。那麼你可以使用

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

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