gpt4 book ai didi

ios - 警报字段关闭一个

转载 作者:行者123 更新时间:2023-11-29 01:14:02 26 4
gpt4 key购买 nike

我正在构建一个示例 map 应用程序,用户可以在其中放置图钉并使用适当的描述对其进行标记。它接近工作,但描述有一个偏差。也就是说,我放置的第一个图钉将具有默认文本,第二个图钉将具有我为第一个图钉输入的描述,依此类推。

我一直在努力处理这段代码,我意识到还有其他问题。一旦我让它工作,我将把它放在代码审查中,但请随时指出任何其他错误。

我已经突出显示了相关方法,但在 ViewController 中包含了所有内容。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

var locationManager = CLLocationManager()
var location: CLLocation!
var newAnnotation: String?

@IBOutlet weak var map: MKMapView!

@IBAction func refreshMap(sender: AnyObject) {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
location = locationManager.location
locationManager.stopUpdatingLocation() //not my question right now but I'm pretty sure this is dumb
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

self.map.setRegion(region, animated: true)
}

override func viewDidLoad() {
super.viewDidLoad()
refreshMap(self.map)
let uilpgr = UILongPressGestureRecognizer(target: self, action: "addPlace:")
uilpgr.minimumPressDuration = 1
map.addGestureRecognizer(uilpgr)
}

//this is the method in question
func addPlace(gestureRecognizer:UIGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = gestureRecognizer.locationInView(self.map)
let newCoordinate: CLLocationCoordinate2D = map.convertPoint(touchPoint, toCoordinateFromView: self.map)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
let alert = UIAlertController(title: "New Memorable Place", message: "Enter a description for this location", preferredStyle: .Alert)

alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "Description"
})

alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.newAnnotation = alert.textFields![0].text
}))

self.presentViewController(alert, animated: true, completion: nil)

annotation.title = newAnnotation
map.addAnnotation(annotation)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last! as CLLocation

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

self.map.setRegion(region, animated: true)
}

}

最佳答案

您应该将 AddPlace 方法的最后两行(将注释添加到 map 中)移动到警报的操作处理程序中。操作处理程序将在将来的某个时间异步调用。

关于ios - 警报字段关闭一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com