1

我试图在RightCalloutAccessoryView的DetailDisclosure按钮被点击时,将ViewController1(VC1)中所选MKAnnotation的坐标,标题和副标题传递给ViewController2(VC2)。我有一个从VC1到VC2的标识符viaSegue。我在VC2中有一个带标识符viaSegueLabel的标签,我想将坐标显示为字符串。将选定注释的坐标传递给新视图控制器Swift 2.0

是定制MKAnnotation的召唤出来,以便它显示在rightCalloutAccessoryView一个DetailDisclosure按钮看起来功能,如:

// Customize Annotation Callout 
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     // 1 
     let identifier = "Capital" 

     // 2 
     if annotation.isKindOfClass(Capital.self) { 
      // 3 
      var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

      if annotationView == nil { 
       //4 
       annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier) 
       annotationView!.canShowCallout = true 

       // 5 
       let btn = UIButton(type: .DetailDisclosure) 
       annotationView!.rightCalloutAccessoryView = btn 
      } else { 
       // 6 
       annotationView!.annotation = annotation 
      } 

      return annotationView 
     } 

     // 7 
     return nil 
    } 

这需要用户从VC1到VC2当DetailDisclosure按钮被点击貌似功能:

// When righCalloutAccessoryView is tapped, segue to newView 
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    self.performSegueWithIdentifier("newView", sender: view) 
} 

而且我觉得我需要实现来完成这个看起来像函数:

// Pass data to newView 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if (segue.identifier == "newView") { 
     let destViewController:BusStopSettingsViewController = segue.destinationViewController as! BusStopSettingsViewController 
     destViewController.viaSegue = // not sure how to reference selected Annotation here 
    } 
} 

在prepareForSegue()的最后一行中,我需要引用当前选定的MKAnnotation。是否有内置Swift的方法可以让我这样做,还是应该使Annotation成为全局的?

回答

0

在未来任何人需要实现类似的东西的情况下能够弄清楚。

self.performSegueWithIdentifier("newView", sender: view) 

编程塞格斯你的程序连接到VC您目前在通过与标识符"newView"一个赛格瑞视图控制器(VC)。在它完全停止之前,程序调用prepareForSegue()。这个函数是你将处理发送信息给你继续使用的VC的地方。我的问题是,我不知道我发送的是什么(在类型,变量名称等方面)。如果您注意,prepareForSegue()self.performSegueWithIdentifier("newView", sender: view)都有参数发件人。您使用performSegueWithIdentifier()发送的内容将被传入prepareForSegue(),并将通过名称为viaSegue的变量在您的destinationViewController中收到。这不是一个标准名称,而是我选择命名该变量的名称,如果您研究上面的代码,则会看到它的使用位置以及它的工作原理。

所以我想发送关于我已经挖掘的MKAnnotation的信息。所以,我需要发送一个MKAnnotationView类型的对象到我的接收VC“BusStopSettingsViewController”(BSSVC)。在BSSVC中,我需要一个名为“viaSegue”的MKAnnotationView类型的变量。为了MKAnnotationView我需要做的

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if (segue.identifier == "newView") { 
     // pass data to next view 
     let destViewController:BusStopSettingsViewController = segue.destinationViewController as! BusStopSettingsViewController 
     destViewController.viaSegue = sender as! MKAnnotationView 
    } 
} 

通知viaSegue是如何指定为将要接收这个对象变量发送BSSVC类型的对象。

希望这会有所帮助!