【发布时间】:2019-10-09 10:45:48
【问题描述】:
我在整个 UIViewController 方面遇到了一些问题。我的想法是,viewDidLoad() 类似于其他语言中的 main(),但特别是在这种情况下,我看不到 viewDidLoad() 函数中调用的任何函数。
首先我完全被 var locationManager 弄糊涂了,它实际上是一个 CLLocationManager 和一个 func。怎么样?
我在哪里调用 func locationManager?我可以返回 locValue.latitude 和 locValue.longitude 吗?如何在 viewDidLoad() 中捕获它们?最后,我想在按下按钮后将这两个参数发送给某物(请参阅:func SendButtonAction)。 但我的问题是,我不知道如何将这两个家伙从 func locationManager 的主体带到 func SendButtonAction 中的输入。
感谢任何帮助:) 我想我需要更多的基础知识。
import UIKit
import MapKit
import CoreLocation
class GPSNew: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var zurueckButton: UIButton!
@IBOutlet weak var SendButton: UIButton!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
// Do any additional setup after loading the view.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
let locValue: CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let userLocation = locations.last
let viewRegion = MKCoordinateRegion(center: (userLocation?.coordinate)!, latitudinalMeters: 600, longitudinalMeters: 600)
self.mapView.setRegion(viewRegion, animated: true)
//return (locValue.latitude, locValue.longitude)
}
@IBAction func SendButtonAction(_ sender: Any) {
//send the user location to something
//end updating location
locationManager.stopUpdatingLocation()
}
}
【问题讨论】:
-
函数名以小写开头
标签: ios swift function class core-location