2016-07-28 50 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以及段落。 –