2016-07-28 53 views
0

我在我的mapview上放置了兩個不同的引腳。我有每個信息按鈕。信息按鈕將延續到具有圖像視圖(用於保存該地點的圖片)和文本標籤(用於保存關於該地點的信息)的UIViewController。針對不同引腳標註的不同信息

我的問題是我怎樣才能生成的信息和圖片取決於哪個引腳註釋按鈕被選中。最後一個函數是爲了延續到信息視圖控制器而使用的函數。

class GetToTheStart: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 
//map view outlet 
@IBOutlet weak var mapView: MKMapView! 

//defining use of location manager 
let myLocMgr = CLLocationManager() 




    override func viewDidLoad() { 
    super.viewDidLoad() 

    //setting up location request 
    myLocMgr.desiredAccuracy = kCLLocationAccuracyBest 
    myLocMgr.requestWhenInUseAuthorization() 
    myLocMgr.startUpdatingLocation() 
    myLocMgr.delegate = self 
    mapView.delegate = self 

    // coordinates of desired locations for pins 
    var zoo1 = CLLocationCoordinate2DMake(53.347439, -6.291820) 
    var town1 = CLLocationCoordinate2DMake(53.347247, -6.290865) 

    //setting up pin 1 annotation (the zoo) 
    var zoopin = MKPointAnnotation() 
    zoopin.coordinate = zoo1 
    zoopin.title = "Dublin Zoo" 
    zoopin.subtitle = "This this the zoo" 
    mapView.addAnnotation(zoopin) 

    //setting up pin 2 annotation (the town) 
    var townpin = MKPointAnnotation() 
    townpin.coordinate = zoo1 
    townpin.title = "Dublin town" 
    townpin.subtitle = "This this the town" 
    mapView.addAnnotation(townpin) 
} 


    //setting up Pin callout button for segue 
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { 
    } 
    let reuseIdentifier = "pin" 
    var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView 
    if pin == nil { 
     pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier) 
     pin!.pinColor = .Red 
     pin!.canShowCallout = true 
     pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
    } else { 
     pin!.annotation = annotation 
    } 
    return pin 
} 



    //performing segue from info button to infoViewController 
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    performSegueWithIdentifier("info", sender: view) 
} 
+0

同時給予代碼,格式得好否則將很難通過它去...... –

+0

當你點擊你需要知道哪個引腳註釋點擊任一引腳標題爲「都柏林動物園」的引腳或「都柏林小鎮」?這是你的問題嗎? –

+0

重新格式化它,並添加了一些評論,所以它應該更容易遵循。是的問題是我不知道如何發送信息只關於被點擊的引腳(即動物園引腳或城市引腳)到infoViewController。 –

回答

1

爲此,您需要覆蓋下面的方法。在這裏我們將得到annotationView,它將觸發segue。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "info") { 
     if let annotation = sender as? MKAnnotationView { 
      let detailViewController = segue.destinationViewController as! DetailViewController 
      detailViewController.titleText = annotation.annotation?.title ?? "" 
      detailViewController.detaileText = annotation.annotation?.subtitle ?? "" 
     } 
    } 
} 

而在detailViewController是一樣的infoViewController,在這裏我有兩個標籤和我有兩個公共變量。這只是爲了避免錯誤,因爲此時我們沒有標籤對象。

這是我的DetailViewController的代碼。

import UIKit 

class DetailViewController: UIViewController { 

@IBOutlet weak var titleLabel: UILabel! 
@IBOutlet weak var detailLabel: UILabel! 


var titleText: String? { didSet { updateUI() } } 
var detaileText: String? { didSet { updateUI() } } 

override func viewDidLoad() { 
    super.viewDidLoad() 
    updateUI() 
} 

private func updateUI() { 
    self.titleLabel?.text = self.titleText 
    self.detailLabel?.text = self.detaileText 
} 
} 
+0

非常感謝您能夠區分不同的針腳。但是,這種方式只會發送標題和副標題。有沒有辦法將每段信息鏈接到每個引腳並顯示它?我正在考慮將副標題設置爲信息段落,並將其從註釋中隱藏起來,您會怎麼看?是否有可能使用這種方法鏈接圖像與每個引腳? –

+0

你可以更具體..我沒有得到 –

+0

很酷。好的,您發佈的代碼可以將所選引腳的標題和副標題發送到infoViewController。這非常有幫助。不過,我想發送一個關於所選引腳的段落給infoViewController,而不僅僅是引腳的標題和副標題。我也想有一個圖片與每個引腳相關聯,並能夠發送到infoViewController以及段落。 –