[英]Swift Use AlertController to add annotation on mapView
I'm trying to use an alert controller with a textField
to create a mapView
annotation using the textField
as the title. 我正在尝试将警报控制器与textField
结合使用,以使用textField
作为标题创建mapView
批注。 I can print all the constants I've created but the annotation doesn't work. 我可以打印所有已创建的常量,但注释不起作用。 When I use the annotation code outside the alertAction
it works fine, but then I can't get the textField
input. 当我在alertAction
之外使用注释代码时,它可以正常工作,但随后无法获取textField
输入。 What am I doing wrong? 我究竟做错了什么?
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)
}
Just store the touch points before UIAlertController Presentation and use that in UIAlterAction because when Alert popover/present while longpress it cannot able to save/get touch point that why touch points inside UIAlterAction is always 0,0. 只需将接触点存储在UIAlertController Presentation之前,并在UIAlterAction中使用它,因为当Alert弹出/长按时呈现时,它无法保存/获取接触点,这就是为什么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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.