Sam Aubin Sam Aubin - 1 year ago 111
Swift Question

Swift- segue between views with map annotation button

I relatively new to programming and need help switching between views by tapping a button in a mapView annotated pin.

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var bottompnl: UIImageView!
@IBAction func mysticsbtn(sender: AnyObject) {
@IBAction func bondibtn(sender: AnyObject) {

let locationManager = CLLocationManager()

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let identifier = "beach"
if annotation is Beach {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
let btn = UIButton(type: .DetailDisclosure)
annotationView!.rightCalloutAccessoryView = btn

} else {
annotationView!.annotation = annotation
return annotationView
self.performSegueWithIdentifier("mysticssegue", sender: nil)
return nil

override func viewDidLoad() {

let annotations = getMapAnnotations()

// Add mappoints to Map
mapView.delegate = self

self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.mapView.showsUserLocation = true

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

// MAP: Location Delegates Methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let centre = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: centre, span: MKCoordinateSpan(latitudeDelta: 3, longitudeDelta: 3))
self.mapView.setRegion(region, animated: true)

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Erros: " + error.localizedDescription)

//MARK:- Annotations

func getMapAnnotations() -> [Beach] {
var annotations:Array = [Beach]()

//load plist file

var beaches: NSArray?
if let path = NSBundle.mainBundle().pathForResource("beaches", ofType: "plist") {
beaches = NSArray(contentsOfFile: path)
if let items = beaches {
for item in items {
let lat = item.valueForKey("lat") as! Double
let long = item.valueForKey("long")as! Double
let annotation = Beach(latitude: lat, longitude: long)
annotation.title = item.valueForKey("title") as? String

return annotations

Answer Source

It looks like you're returning from the function mapView before calling performSegueWithIdentifier.

return annotationView
self.performSegueWithIdentifier("mysticssegue", sender: nil)

Try reversing the order of these two lines. I also noticed that you have some @IBAction methods that have no code in them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download