gpt4 book ai didi

ios - Swift:MapView calloutAccessoryControlTapped 索引

转载 作者:行者123 更新时间:2023-11-30 11:49:31 25 4
gpt4 key购买 nike

class MapViewController: UIViewController, MKMapViewDelegate, HomeModelProtocol {

var feedItems: NSArray = NSArray()
var selectedLocation : LocationModel = LocationModel()


@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

let initialLocation = CLLocation(latitude: 45.444958, longitude: 12.328463)

centerMapLocation(location: initialLocation)
mapView.delegate = self

let homeModel = HomeModel()
homeModel.delegate = self
homeModel.downloadItems()


}

func itemsDownloaded(items: NSArray) {
feedItems = items

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

let regionRadus: CLLocationDistance = 1000
func centerMapLocation(location: CLLocation){
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadus, regionRadus)
mapView.setRegion(coordinateRegion, animated: true)
}

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)
//checkLocationAuthorizationStatus()
displayLocations()
}
func displayLocations(){

let i = feedItems.count
var x = 0
while x<i{
let item: LocationModel = feedItems[x] as! LocationModel

var poiCoodinates = CLLocationCoordinate2D()

poiCoodinates.latitude = CDouble(item.latitude!)!
poiCoodinates.longitude = CDouble(item.longitude!)!


let pin: MKPointAnnotation = MKPointAnnotation()
pin.coordinate = poiCoodinates
self.mapView.addAnnotation(pin)

pin.title = item.name
pin.subtitle = item.address
x = x+1
}
//return loc
}

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

let view = MKMarkerAnnotationView(annotation: selectedLocation as? MKAnnotation, reuseIdentifier: "pin")
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.leftCalloutAccessoryView = UIButton(type: .detailDisclosure)
return view
}

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

print(control.tag)
selectedLocation = feedItems[0] as! LocationModel
performSegue(withIdentifier: "InformationSegue", sender: self)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

// Get reference to the destination view controller
let detailVC = segue.destination as! InformationViewController
// Set the property to the selected location so when the view for
// detail view controller loads, it can access that property to get the feeditem obj
detailVC.selectedLocation = selectedLocation
}
}

这是我的代码。我想在下一个 View Controller 中显示位置。我需要获取 feeditems[] 处的索引。我如何获取索引:

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

那么我如何获取点击哪个按钮的索引。 map 中放置了许多对象。

感谢您的帮助,并对我的英语不好表示歉意,希望你们理解我。

最佳答案

1.定义子类MKPointAnnotation。

class MyPointAnnotation: MKPointAnnotation {
var feedItem: LocationModel
}

2.将MyPointAnnotation.feedItem设置为feedItem。

let item: LocationModel = feedItems[x] as! LocationModel

var poiCoodinates = CLLocationCoordinate2D()
poiCoodinates.latitude = CDouble(item.latitude!)!
poiCoodinates.longitude = CDouble(item.longitude!)!

let pin: MyPointAnnotation = MyPointAnnotation()
pin.coordinate = poiCoodinates
pin.feedItem = item // Important!
self.mapView.addAnnotation(pin)

3.在calloutAccessoryControlTapped委托(delegate)方法中获取feedItem

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if let pin = view.annotation as? MyPointAnnotation {
print(pin.feedItem)
}
}

关于ios - Swift:MapView calloutAccessoryControlTapped 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48479310/

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