gpt4 book ai didi

ios - 在 MKAnnotationView 中点击按钮打开 NavigationController

转载 作者:行者123 更新时间:2023-11-28 08:34:53 25 4
gpt4 key购买 nike

我有 MKAnnotationView,我在其中显示标题、副标题和信息按钮,点击位置图钉。

我添加了以下代码

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

if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}


let reuseId = "pin"
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView.canShowCallout = true
pinView.animatesDrop = true
pinView.pinTintColor = UIColor.darkGrayColor()
pinView.draggable = true
let btn = UIButton(type: .DetailDisclosure)
pinView.rightCalloutAccessoryView = btn

let tapGesture = UITapGestureRecognizer(target: self,action: #selector(MapView.calloutTapped(_:)))
pinView.addGestureRecognizer(tapGesture)

return pinView
}

func calloutTapped(sender: UITapGestureRecognizer) {
// if sender.state != UIGestureRecognizerState.Began { return }
let annView: MKAnnotationView! = sender.view as? MKAnnotationView
let ann:MKAnnotation! = annView!.annotation
print("handlePinButtonTap: ann.title \(ann!.title!!) and \(ann!.subtitle!!)")
let touchLocation = sender.locationInView(mapView)
let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView)
print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude) " )

let storyboard : UIStoryboard = UIStoryboard(name: "ShoppingCart", bundle: nil)
let vc : ShoppingCartController = storyboard.instantiateViewControllerWithIdentifier("ShoppingCart") as! ShoppingCartController


let navigationController = UINavigationController(rootViewController: vc)

self.presentViewController(navigationController, animated: true, completion: nil)



}

但是在使用此代码时,点击 map 上的图钉,用户将导航到 ShoppingCart Storyboard。我想在点击信息按钮时显示 ViewController 以及所点击事件的标题、副标题、纬度和经度。

最佳答案

这是有效的解决方案

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

if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}


let reuseId = "pin"
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView.canShowCallout = true
pinView.animatesDrop = true
pinView.pinTintColor = UIColor.darkGrayColor()
pinView.draggable = true
let btn = UIButton(type: .DetailDisclosure)
pinView.rightCalloutAccessoryView = btn

let tapGesture = UITapGestureRecognizer(target: self,action: #selector(MapView.calloutTapped(_:)))
pinView.addGestureRecognizer(tapGesture)

return pinView
}

func calloutTapped(sender: UITapGestureRecognizer) {


// if sender.state != UIGestureRecognizerState.Began { return }
let annView: MKAnnotationView! = sender.view as? MKAnnotationView
let ann:MKAnnotation! = annView!.annotation
print("handlePinButtonTap: ann.title \(ann!.title!!) and \(ann!.subtitle!!)")
let touchLocation = sender.locationInView(mapView)
let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView)
print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude) " )
}

关于ios - 在 MKAnnotationView 中点击按钮打开 NavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38071509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com