gpt4 book ai didi

Swift-使用 map 注释按钮在 View 之间进行segue

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

我对编程相对较新,需要通过点击 mapView 带注释的图钉中的按钮来帮助在 View 之间切换。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
//REF

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var bottompnl: UIImageView!
@IBAction func mysticsbtn(sender: AnyObject) {
}
@IBAction func bondibtn(sender: AnyObject) {
}

//MAP
let locationManager = CLLocationManager()

//Annotation
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let identifier = "beach"
if annotation is Beach {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
let btn = UIButton(type: .DetailDisclosure)
annotationView!.rightCalloutAccessoryView = btn

} else {
annotationView!.annotation = annotation
}
return annotationView
self.performSegueWithIdentifier("mysticssegue", sender: nil)
}
return nil
}

override func viewDidLoad() {
super.viewDidLoad()

let annotations = getMapAnnotations()

// Add mappoints to Map
mapView.addAnnotations(annotations)
mapView.delegate = self

//MAP
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
//MAKES THE DOT
self.mapView.showsUserLocation = true
}

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

// MAP: Location Delegates Methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let centre = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: centre, span: MKCoordinateSpan(latitudeDelta: 3, longitudeDelta: 3))
self.mapView.setRegion(region, animated: true)
self.locationManager.startUpdatingLocation()
}

//TO FIND ERROS
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Erros: " + error.localizedDescription)
}

//MARK:- Annotations

func getMapAnnotations() -> [Beach] {
var annotations:Array = [Beach]()

//load plist file

var beaches: NSArray?
if let path = NSBundle.mainBundle().pathForResource("beaches", ofType: "plist") {
beaches = NSArray(contentsOfFile: path)
}
if let items = beaches {
for item in items {
let lat = item.valueForKey("lat") as! Double
let long = item.valueForKey("long")as! Double
let annotation = Beach(latitude: lat, longitude: long)
annotation.title = item.valueForKey("title") as? String
annotations.append(annotation)
}
}

return annotations
}
//END
}

最佳答案

看起来您是在调用 performSegueWithIdentifier 之前从函数 mapView 返回的。

return annotationView
self.performSegueWithIdentifier("mysticssegue", sender: nil)

尝试颠倒这两行的顺序。我还注意到您有一些 @IBAction 方法,其中没有代码。

关于Swift-使用 map 注释按钮在 View 之间进行segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39305044/

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