我正在观看有关如何在Swift中编辑地图并添加注释的课程,但不幸的是教师在地图上添加注释后没有谈论如何编辑注释的标题和副标题,所以我想去更进一步,用我在Swift中的简单知识编辑它们。
以下是代码:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
//add
@IBOutlet weak var titleText: UITextField!
@IBOutlet weak var subtitleText: UITextField!
@IBOutlet weak var buttonAdd: UIButton!
//add
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//add
subtitleText.alpha = 0
titleText.alpha = 0
buttonAdd.hidden = true
//add
// 51.498340, -0.153257
var latitude:CLLocationDegrees = 51.498340
var longitude:CLLocationDegrees = -0.153257
var latDelta:CLLocationDegrees = 0.02
var lonDelta:CLLocationDegrees = 0.02
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Belgravia"
annotation.subtitle = "I love you!"
mapView.addAnnotation(annotation)
var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
uilpgr.minimumPressDuration = 2.0
mapView.addGestureRecognizer(uilpgr)
}
let newAnnotation = MKPointAnnotation()
func action(gestureRecognizer:UIGestureRecognizer) {
var touchPoint = gestureRecognizer.locationInView(self.mapView)
var newCoordinate:CLLocationCoordinate2D = mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
newAnnotation.coordinate = newCoordinate
titleText.alpha = 1
subtitleText.alpha = 1
buttonAdd.hidden = false
mapView.addAnnotation(newAnnotation)
var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
uilpgr.minimumPressDuration = 2.0
}
func addPoint(sender: AnyObject) {
newAnnotation.title = titleText.text
newAnnotation.subtitle = subtitleText.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
每当我尝试在这两个文本中写入文本然后按下iOS模拟器中的addPoint按钮时,它会意外崩溃
[Maps.ViewController按钮:]:无法识别的选择器发送到实例...