2017-08-14 1 views
0

Ich versuche, einen Alarmcontroller mit einer textField zu verwenden, um eine mapView Annotation mit dem textField als Titel zu erstellen. Ich kann alle Konstanten drucken, die ich erstellt habe, aber die Anmerkung funktioniert nicht. Wenn ich den Anmerkungscode außerhalb der alertAction verwende, funktioniert es gut, aber dann kann ich den textField Eingang nicht bekommen. Was mache ich falsch?Swift Verwenden von AlertController zum Hinzufügen von Annotationen in 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) 

} 

Antwort

0

speichern Sie die Berührungspunkte vor UIAlertController Präsentation und verwenden, die in UIAlterAction, weil, wenn Alarm popover/present während Longpress kann es nicht in der Lage Berührungspunkt zu speichern/bekommen, warum Berührungspunkte innerhalb UIAlterAction ist immer 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) 

} 
Verwandte Themen