如何使用swift2

时间:2017-01-27 09:14:54

标签: ios swift3 mkmapview mkannotation

任何人都可以帮我添加不同的按钮动作到第二个注释/注释(注释2),现在按钮在两个注释引脚中做同样的工作,如何相互做不同的工作。我在我的项目中使用Swift3,这是我的代码。谢谢

这是我的代码。

  import UIKit
  import MapKit
  import CoreLocation

 class MyAnnotation: MKPointAnnotation {
      var uniqueId: Int!
 }

  class LocationViewController: UIViewController , MKMapViewDelegate , CLLocationManagerDelegate{

    @IBOutlet weak var map: MKMapView!{

    didSet{
        map.delegate = self
    }
}

@IBOutlet weak var locationInfo: UITextView!


override func viewDidLoad() {
    super.viewDidLoad()


    let locations = CLLocationCoordinate2DMake(33.314627, 44.303500)

    let location2 = CLLocationCoordinate2DMake(33.312149, 44.3024567)

    let span = MKCoordinateSpanMake(0.02, 0.02)

    let span2 = MKCoordinateSpanMake(0.02, 0.02)

    let region = MKCoordinateRegionMake(locations, span)

     let region2 = MKCoordinateRegionMake(location2, span2)


    map.setRegion(region, animated: true)

    map.setRegion(region2, animated: true)


    let annotation = MyAnnotation()
    //annotation.setCoordinate(location)
    annotation.coordinate = locations
    annotation.title = "Zaid Homes"
    annotation.subtitle = "Hay aljameaa"

    annotation.uniqueId = 1

    map.addAnnotation(annotation)

    let annotation2 = MyAnnotation()
    //annotation.setCoordinate(location)
    annotation2.coordinate = location2
    annotation2.title = "Zaid "
    annotation2.subtitle = "aljameaa"

    annotation.uniqueId = 2

    map.addAnnotation(annotation2)

    //Showing the device location on the map
    self.map.showsUserLocation = true;

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView Id")
    if view == nil{
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView Id")
        view!.canShowCallout = true
    } else {
        view!.annotation = annotation
    }

    view?.leftCalloutAccessoryView = nil
    view?.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure)


    return view
}

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

    if (control as? UIButton)?.buttonType ==   UIButtonType.detailDisclosure {
        mapView.deselectAnnotation(view.annotation, animated: false)
        if let myAnnotation = view.annotation as? MyAnnotation {
            if (myAnnotation.uniqueId == 1) {
                performSegue(withIdentifier: "info", sender: view)
            }
            else  {
                performSegue(withIdentifier: "info2", sender: view)
            }
        }            
    }
}

}

2 个答案:

答案 0 :(得分:1)

你可以通过创建两个子类MKPointAnnotation来做到这一点,然后在委托方法中你可以这样做:

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

if view is subClass1 {
    // do action for subclass 1

}
else if view is subClass2 {
   // do action for subClass 2
}
}

如果这可以解决您的问题,请告诉我。

<强>更新

你可以使你委托的实现更简单,例如:

&#13;
&#13;
class ClassA:MKPointAnnotation{
    func doActionWhenCalloutTapped(){
        //do some action
    }
}

class ClassB:ClassA{
    override func doActionWhenCalloutTapped(){
        //do some actions for annotation of type B
    }
}

class ClassC:ClassA{
    override func doActionWhenCalloutTapped(){
        //do some actions for annotation of type C
    }
}

func viewDidLoad(){
    super.viewDidLoad()
    let annotation = ClassB()
    //annotation.setCoordinate(location)
    annotation.coordinate = locations
    annotation.title = "Zaid Homes"
    annotation.subtitle = "Hay aljameaa"
    
    map.addAnnotation(annotation)
    
    let annotation2 = ClassC
    //annotation.setCoordinate(location)
    annotation2.coordinate = location2
    annotation2.title = "Zaid "
    annotation2.subtitle = "aljameaa"
    
    map.addAnnotation(annotation2)
}

func mapView(_ mapView: MKMapView, annotationView view:  MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    
    (view.annotation as! ClassA).doActionWhenCalloutTapped()
   
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

了解您点击哪个注释的最简单方法是使用创建自定义注记类并添加注释。因此,创建一个注释类MyAnnotation子类MKPointAnnotation并使用多个注释维护一个uniqueId。

class MyAnnotation: MKPointAnnotation {
    var uniqueId: Int!
}

现在您需要添加MyAnnotation类型的注释,而不是MKPointAnnotation

let annotation = MyAnnotation()
annotation.coordinate = locations
annotation.title = "Zaid Homes"
annotation.subtitle = "Hay aljameaa"
//Set uniqueId for annotation
annotation.uniqueId = 1    
map.addAnnotation(annotation)

let annotation2 = MyAnnotation()
annotation2.coordinate = location2
annotation2.title = "Zaid "
annotation2.subtitle = "aljameaa"
//Set uniqueId for annotation
annotation2.uniqueId = 2    
map.addAnnotation(annotation2)

现在,在uniqueId方法中查看您使用了注释的calloutAccessoryControlTapped

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

    if (control as? UIButton)?.buttonType ==   UIButtonType.detailDisclosure {
        mapView.deselectAnnotation(view.annotation, animated: false)
        if let myAnnotation = view.annotation as? MyAnnotation {
            if (myAnnotation.uniqueId == 1) {
                 performSegue(withIdentifier: "info1", sender: view)
            }
            else  {
                 performSegue(withIdentifier: "info2", sender: view)
            }
        }            
    }
}