在SWIFT中的MapView中选择索引处的对象

时间:2015-03-10 18:56:07

标签: swift mkmapview mkannotation mkannotationview

一旦按下正确的标注按钮,我试图呈现另一个视图控制器,但它必须转到正确的indexPath,类似于在表视图中发生这种情况。这是我的目标:

我已经创建了一个自定义注释:

class annotationCustom: NSObject, MKAnnotation {

    var coordinate : CLLocationCoordinate2D
    var title : NSString!
    var subtitle : NSString!
    var isUserAnnotation : Bool
    var dataImage = [NSData]()

    init(coordinate: CLLocationCoordinate2D, title: NSString!, subtitle: NSString!){

        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
        self.isUserAnnotation = false
    }
}

然后将viewForAnnotation设置为:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        var dqPin = "pin"

        var view = mapView.dequeueReusableAnnotationViewWithIdentifier(dqPin) as? MKPinAnnotationView

                view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: dqPin)
                view!.canShowCallout = true

                    var arrowBut = UIImage(named: "arrow")
                    var arrowButton = UIButton()
                    arrowButton.setImage(arrowBut, forState: UIControlState.Normal)
                    arrowButton.tintColor = UIColor.blackColor()
                    arrowButton.frame = CGRectMake(0, 0, 30, 30)
                    view!.rightCalloutAccessoryView = arrowButton
                   }
             return view
             }

现在我知道我必须使用这个功能:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
            func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

        if control == view.rightCalloutAccessoryView {

        let sb = UIStoryboard(name: "Main", bundle: nil)
        let userProfileVC = sb.instantiateViewControllerWithIdentifier("usersProfileVC") as UsersProfilesViewController

                userProfileVC.profileName = annotationCustom.title // error: 'annotationCustom.Type' does not have a member named 'title'

        performSegueWithIdentifier("goToUserFromMap", sender: self)
    }

}

}

有人可以填补空白或帮助我。

由于

1 个答案:

答案 0 :(得分:0)

而不是:

if control == annotationView.rightCalloutAccessoryView {

它应该是:

if control == view.rightCalloutAccessoryView {

view是参数的内部私有名称,annotationView是调用者可见的外部名称。

但是,在这种情况下,您并不需要首先检查control,因为您的标注只有一个控件(rightCalloutAccessoryView)。他们没有leftCalloutAccessoryView,因此用户只能使用一个控件。

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

    println("disclosure pressed on: \(view.annotation.title)")

}

<小时/> 顺便说一句,代码是viewForAnnotation中显示的实际代码吗?因为看起来dequeueReusableAnnotationViewWithIdentifier的结果会被忽略,并且在}之前会有一个神秘的额外结束return view

<小时/> 这里:

userProfileVC.profileName = annotationCustom.title

annotationCustom的名称 - 而不是它的实例(也就是说,惯例是以大写字母开始类名)。 view.annotation就是这里的实例。将其投放为annotationCustom以访问任何自定义属性(尽管title不是自定义的):

if let ac = view.annotation as? annotationCustom {
    userProfileVC.profileName = ac.title
}