Swift 斯威夫特。使用tabBarController.selectedIndex切换到另一个viewcontroller并传递数据
我是IOS开发新手。我有一个带有两个选项卡的Tabbar控制器。第一项是一张地图,上面标注了一个著名的地方,比如北京的颐和园。当用户触摸注释插针时,注释视图将显示,注释右侧有一个详细信息按钮。当用户单击详细信息按钮时,第二个ViewController将显示一个标签,例如,该标签的文本应为第一个ViewController注释的注释标题或副标题。我成功的是,我可以通过“self.tabBarController.selectedIndex=1”切换到第二个ViewController。 (我没有导航控制器,也没有创建TabBarController类文件) 包含贴图的第一个ViewController:Swift 斯威夫特。使用tabBarController.selectedIndex切换到另一个viewcontroller并传递数据,swift,tabbarcontroller,pass-data,Swift,Tabbarcontroller,Pass Data,我是IOS开发新手。我有一个带有两个选项卡的Tabbar控制器。第一项是一张地图,上面标注了一个著名的地方,比如北京的颐和园。当用户触摸注释插针时,注释视图将显示,注释右侧有一个详细信息按钮。当用户单击详细信息按钮时,第二个ViewController将显示一个标签,例如,该标签的文本应为第一个ViewController注释的注释标题或副标题。我成功的是,我可以通过“self.tabBarController.selectedIndex=1”切换到第二个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
}
}
第二视图控制器:
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.
}
}