0

I was watching a course on how to edit maps in Swift and add annotations, but unfortunately the instructor did not talk about how to edit the title and the subtitle of an annotation after adding one on the map, so I wanted to go a little bit further and edit them with my very simple knowledge in Swift.

Here's the code:

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.
}


}

Every time I try to write text inside these two and then press the addPoint button in the iOS simulator it'll crash unexpectedly

[Maps.ViewController button:]: unrecognized selector sent to instance...

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Yaman KJ
  • 1
  • 1
  • What error-message do you get? – FelixSFD Sep 02 '16 at 19:56
  • This is what appeared after crashing " libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) " Here's the full thing that was printed to the logs: http://imgur.com/a/Z6S9v The debugger output is "lldb" – Yaman KJ Sep 02 '16 at 23:17
  • The error message is `unrecognized selector sent to instance xxx`. So I think the problem is not about adding the annotations. It might be the selector that is called, when the user presses the button. I can't see, where you add the selector for `addPoint(sender:)` to the button you want to press. – FelixSFD Sep 03 '16 at 13:03
  • I've just added a sender to the button `addPoint(sender: UIButton)` but it's still crashing for the same reason :\ – Yaman KJ Sep 03 '16 at 19:19
  • @FelixSFD Any idea? – Yaman KJ Sep 04 '16 at 11:55

0 Answers0