迅速。使用tabBarController.selectedIndex切换到另一个viewcontroller并传递数据

时间:2015-01-25 10:01:13

标签: swift tabbarcontroller pass-data

我是IOS开发的新手。我有一个带有2个标签的TabBarController。第一个TabItem是一个地图,注释了一个着名的地方,如北京的颐和园。当用户触摸注释引脚时,注释视图将显示,并在注释的右侧显示详细信息按钮。当用户单击详细信息按钮时,第二个ViewController将显示一个标签,其标签应为第一个ViewController注释的注释标题或副标题。我成功的是我可以通过`self.tabBarController.selectedIndex = 1切换到第二个ViewController。 (我没有导航控制器,我还没有创建TabBarController类文件)

包含地图的第一个ViewController:

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
    }

}

第二个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.
    }

}

0 个答案:

没有答案