谁打电话prepareForSegue?

时间:2015-03-13 22:05:16

标签: swift view

海派我有问题 在我的应用程序中,我有一个地图,任何注释为MKPointAnnotation

func mapView(mapView: MKMapView!, annotationView: MKAnnotationView!,
    calloutAccessoryControlTapped value: UIControl!)
{
        if value == annotationView.rightCalloutAccessoryView
        {  
            return performSegueWithIdentifier("newSegue", sender: annotationView) 
        }  
}

然后是其他代码

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "newSegue" {
        let destinationViewController = segue.destinationViewController as newViewController}}

我能找到MKpointAnnotation调用performSegueWithIdentifier和prepareForSegue吗? 然后我知道在我的观点中放置了什么样的对象:)

2 个答案:

答案 0 :(得分:0)

如果在destinationViewController中定义了与MKAnnotionView中的属性匹配的属性,则可以在prepareForSegue中执行此操作:

if let senderObject = sender as? MKAnnotationView {
   destinatinViewController.coordinate = senderObject.coordinate
   destinatinViewController.title = senderObject.title
   destinatinViewController.subtitle = senderObject.subtitle
}

答案 1 :(得分:0)

我解决了这个问题:

1)链接MapKitViewControler使用Ctrl键执行SecondViewControler并将segue命名为" MapInfo"。 2)在MapKitViewControler上创建一个变量varToPass 3)在SecondViewControler上创建一个变量varToReceive 4)在MapKitViewControler上使用Dictionary来存储你的数据(可以是我们的自定义类,如上面的MyAnnotation,包含数据点和其他信息):var myDicyionary = [String : MyAnnotation]()

import MapKit
import AddressBook

class MyAnnotation: NSObject, MKAnnotation
{
    let identifier : String
    let title: String
    let subtitle: String
    let coordinate: CLLocationCoordinate2D

    init(identifier: String, title: String, subtitle: String, coordinate: CLLocationCoordinate2D)
    {
        self.identifier = identifier
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate

        super.init()
    }
}

5)例如,在加载MapView上添加点,并使用标识符字段(在本例中为String)来标识单击的引脚:

let annotation = MyAnnotation(identifier: userId, title: userName, subtitle: subtitle, coordinate: location)
self.mapView.addAnnotation(annotation)

6)确定点击了哪个引脚并将其保存在类变量上并强制准备segue调用:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!)
{
    let anotation = view.annotation as! MyAnnotation        

    if (control == view.rightCalloutAccessoryView)
    {
        println("Button Right pressed!")

        self.varToPass = self.myDicyionary[anotation.identifier]

        //Force prepare for segue call
        self.performSegueWithIdentifier("UserInfo", sender: self)
    }
    else if (control == view.leftCalloutAccessoryView)
    {
        println("Button Left pressed!")
    }
}

7)传递你想要的segue的变量

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "MapInfo")
        {
            let mapInfoViewController = segue.destinationViewController as! MapKitViewControler

            mapInfoViewController.varToReceive = self.varToPass
        }
    }

8)变量varToPassvarToReceive可以是String或MyAnnotation等自定义类。