1

I am new to IOS development. I have a TabBarController with 2 tabs. The 1st TabItem is a map with an annotation of one famous place such as Summer Palace in Beijing. When User touches the annotation pin, the annotation view appears with a detailed information button at the right side of the annotation. When users clicks the detailed information button, the 2nd ViewController appears with for example a label whose text should be the annotation title or subtitle of the 1st ViewController annotation. What I have succeeded is I can switch to 2nd ViewController by `self.tabBarController.selectedIndex = 1. (I have no navigation controller and I have not created a TabBarController class file)

The 1st ViewController containing a map:

import UIKit
import MapKit

class ViewController1: UIViewController, MKMapViewDelegate {



    @IBOutlet weak var theMapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        var latitude: CLLocationDegrees = 39.9921996510
        var longitude: CLLocationDegrees = 116.2684305217
        var latDelta: CLLocationDegrees = 10
        var longDelta: CLLocationDegrees = 10
        var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        var churchLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        var theRegion: MKCoordinateRegion = MKCoordinateRegionMake(churchLocation, theSpan)
        theMapView.setRegion(theRegion, animated: true )
        theMapView.zoomEnabled = true
        var annotationYiheyuan = MKPointAnnotation()
        annotationYiheyuan.coordinate = churchLocation
        annotationYiheyuan.title = "The Summer Palace"
        annotationYiheyuan.subtitle = "The Most Famous Garden in Qing Dynasty"

        theMapView.addAnnotation(annotationYiheyuan)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        if control == view.rightCalloutAccessoryView {

            self.tabBarController?.selectedIndex = 1

        }
    }
    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }
        let reuseId = "pin"

        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

        if anView == nil {
            anView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

            anView.canShowCallout = true
            anView.enabled = true
            let button: UIButton =  UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton

            anView.rightCalloutAccessoryView = button

        }
        else {
            anView.annotation = annotation
        }

        return anView
    }

}

The 2nd ViewController:

import UIKit

class ViewController2: UIViewController {


    @IBOutlet weak var lbl2_VC2: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

}
NorthBig
  • 47
  • 1
  • 9

0 Answers0