2017-08-14 48 views
0

我試圖使用帶有textField的警報控制器來創建使用textField作爲標題的mapView註釋。我可以打印所有已創建的常量,但註釋不起作用。當我使用alertAction之外的註釋代碼時,它工作正常,但是我無法獲得textField輸入。我究竟做錯了什麼?快速使用AlertController在mapView上添加註釋

override func viewDidLoad() { 
    super.viewDidLoad() 

    let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:))) 
    uilpgr.minimumPressDuration = 2 
    map.addGestureRecognizer(uilpgr) 

} 

func longpress(gestureRecognizer: UIGestureRecognizer) { 

    let alert = UIAlertController(title: "New Place", message: "Enter a name", preferredStyle: UIAlertControllerStyle.alert) 

    alert.addTextField { (textField: UITextField) in 
     textField.placeholder = "Name" 
    } 

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (UIAlertAction) in 

     if let tempPlace = alert.textFields?[0].text { 
      let place = tempPlace 
      let touchpoint = gestureRecognizer.location(in: self.map) 
      let coordinate = self.map.convert(touchpoint, toCoordinateFrom: self.map) 
      let annotation = MKPointAnnotation() 
      let latitude = coordinate.latitude 
      let longitude = coordinate.longitude 

      annotation.title = place 
      annotation.subtitle = "Lat " + (String(format: "%.2f", latitude) + " Lon " + String(format: "%.2f", longitude)) 
      annotation.coordinate = coordinate 
      self.map.addAnnotation(annotation) 
     } 


    } 

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (UIAlertAction) in 

    } 


    alert.addAction(okAction) 
    alert.addAction(cancelAction) 

    present(alert, animated: true, completion: nil) 

} 

回答

0

只是存儲UIAlertController演示前的接觸點,並使用在UIAlterAction因爲當警報酥料餅/存在,而長按它可以不能夠保存/獲取觸摸點爲什麼裏面UIAlterAction接觸點始終是0,0。

func longpress(gestureRecognizer: UIGestureRecognizer) { 

let touchpointtemp = gestureRecognizer.location(in: self.map_home) 

let alert = UIAlertController(title: "New Place", message: "Enter a name", preferredStyle: UIAlertControllerStyle.alert) 

    alert.addTextField { (textField: UITextField) in 
     textField.placeholder = "Name" 
    } 

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (UIAlertAction) in 

     if let tempPlace = alert.textFields?[0].text { 
      let place = tempPlace 
      let touchpoint = touchpointtemp 
      let coordinate = self.map_home.convert(touchpoint, toCoordinateFrom: self.map_home) 

      print(coordinate) 
      let annotation = MKPointAnnotation() 
      let latitude = coordinate.latitude 
      let longitude = coordinate.longitude 

      annotation.title = place 
      annotation.subtitle = "Lat " + (String(format: "%.2f", latitude) + " Lon " + String(format: "%.2f", longitude)) 
      annotation.coordinate = coordinate 
      self.map_home.addAnnotation(annotation) 
     } 


    } 

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (UIAlertAction) in 

    } 


    alert.addAction(okAction) 
    alert.addAction(cancelAction) 

    present(alert, animated: true, completion: nil) 

}