iosswiftuiviewcontrollermkmapview

Showing a new viewController when tapping on a mark on a mapView?


I have a mapView with pins showing users positions, tapping on them I show some info about users and places. I'd like to implement a feature in which by tapping on those info little panel, I can navigate to another viewController from which one can send a personal message via Parse. Even a button could be fine.

enter image description here

this is my mapViwController code:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
    {
        let point = PFGeoPoint(latitude:manager.location.coordinate.latitude, longitude:manager.location.coordinate.longitude)
        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: 1, longitudeDelta: 1))
        
        self.mapView.setRegion(region, animated: true)
        
        CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in
            
            if (error != nil)
            {
                println("Error: " + error.localizedDescription)
                return
            }
            
            if placemarks.count > 0
            {
                let pm = placemarks[0] as! CLPlacemark
                self.displayLocationInfo(pm, point: point)
            }
            else
            {
                println("Error with the data.")
            }
        })
    }
    
    func displayLocationInfo(placemark: CLPlacemark, point: PFGeoPoint)
    {
        self.locationManager.stopUpdatingLocation()
        
        self.createAnnotations(point, address: "\(placemark.locality) \(placemark.administrativeArea) \(placemark.postalCode) \(placemark.country)")
    }
    
    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
    {
        println("Error: " + error.localizedDescription)
    }
    
    // MARK: - Create Annotation
    
    func createAnnotations(point: PFGeoPoint, address: String)
    {
        var query = PFUser.query()
        
        var withinKms = NSUserDefaults.standardUserDefaults().objectForKey("withinKms") as! Double
        
//        query?.whereKey("location", nearGeoPoint: point, withinKms: withinKms) // also withinKilometers if wanted
        query?.whereKey("location", nearGeoPoint: point, withinKilometers: withinKms)

        query?.limit = 10
        
        query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
            
            if error == nil
            {
                for(var i = 0; i < objects!.count; i++)
                {
                    let user = objects![i] as! PFUser
                    var myHomePin = MKPointAnnotation()
                    let userPoint = user["location"] as! PFGeoPoint
                    myHomePin.coordinate = CLLocationCoordinate2DMake(userPoint.latitude, userPoint.longitude)
                    myHomePin.title = user.username
                    
                    myHomePin.subtitle = address
                    self.mapView.addAnnotation(myHomePin)
                }
            }
            else
            {
                println("Error: " + error!.localizedDescription)
            }
            
        })
    }

Anbu's Answer

 @IBOutlet weak var mapView: MKMapView!


//************************************************
    
    func mapView(mapView: mapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        
        println("disclosure pressed: \(view.annotation.title)")
        
        // do your stuff here
//        @IBAction func startChat(sender: UIButton)

        // add your segue
        if control == annotationView.rightCalloutAccessoryView {
            self.performSegueWithIdentifier("nextViewController", sender: self)
        }
        
    }
//************************************************

Solution

  • receive mapView delegate callback method

    //if you pressed the annotation view the following method is called
    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
    
    // if you press the calloutButton the following method is called
    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!)
    

    for example

    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    
    println("disclosure pressed: \(view.annotation.title)")
    
    // do your stuff here
    
     // add your segue
    if control == annotationView.rightCalloutAccessoryView {
        self.performSegueWithIdentifier("nextViewController", sender: self)
    }
    
    }