私はIOS開発に不慣れです。2 つのタブを持つ TabBarController があります。1 つ目の TabItem は、北京の頤和園などの 1 つの有名な場所の注釈を含むマップです。ユーザーが注釈ピンをタッチすると、注釈ビューが表示され、注釈の右側に詳細情報ボタンが表示されます。ユーザーが詳細情報ボタンをクリックすると、2 番目の ViewController が表示されます。たとえば、テキストが 1 番目の ViewController アノテーションのアノテーション タイトルまたはサブタイトルであるラベルが表示されます。私が成功したのは、「self.tabBarController.selectedIndex = 1」で 2 番目の 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
}
}
2 番目の 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.
}
}