iOS-一个班级的diDupDateLocations在一个班级中被调用,而不是另一个类



嗨,我正在制作一个程序,以获取用户位置并在地图上放置注释。我首先在"视图控制器"中编写所有代码,然后完美地获得位置。以下是视图控制器中的工作代码。

class MapViewController: UIViewController, CLLocationManagerDelegate {
    var annotation = MKPointAnnotation()
    var userLocation: CLLocation?
    @IBOutlet weak var mapView: MKMapView!
    var locationManager:CLLocationManager!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        determineCurrentLocation()
    }
    func determineCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       userLocation = locations[0] as CLLocation
        print("user latitude = (userLocation?.coordinate.latitude)")
        print("user longitude = (userLocation?.coordinate.longitude)")
        annotation.coordinate = CLLocationCoordinate2D(latitude: (userLocation?.coordinate.latitude)!, longitude: (userLocation?.coordinate.longitude)!)
        annotation.title = "You"
        mapView.addAnnotation(annotation)

    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error (error)")
    }

现在,当我尝试在另一个Swift文件中重新创建几乎完全相同的代码时。didupdatelecations从未被调用。LocationManager.startupdatingLocation((确实被调用。

下面是我从视图控制器调用的新的Swift文件。我在这里有没有简单的概念,因为我真的不明白为什么这不起作用。

import Foundation
import CoreLocation

class SendLocation:  NSObject, CLLocationManagerDelegate {
    var userLocation: CLLocation?
    var locationManager:CLLocationManager!
    func sendLocationPost() {
        determineCurrentLocation()
    }
    func determineCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        print("WHY")
        if CLLocationManager.locationServicesEnabled(){
            locationManager.startUpdatingLocation()
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        userLocation = locations[0] as CLLocation
        print("user latitude = (userLocation?.coordinate.latitude)")
        print("user longitude = (userLocation?.coordinate.longitude)")
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error (error)")
    }
}

我使用以下方式称呼它:

     let location = SendLocation()
    location.sendLocationPost()`

在我的视图控制器中

发生这种情况,因为您不保留对sendlocation对象的引用。

使SendLocation成为您的UiviewController的属性。

例如,从静态范围中调用它不会保留参考。

无法工作:

static func sendLocation() {
    let location = SendLocation()
    location.sendLocationPost()
}

将工作

let location = SendLocation()
func sendLocation() {
    location.sendLocationPost()
}

最新更新