gpt4 book ai didi

swift - 位置服务不会在新的 ViewController 类中运行

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

这里是 Swift 新手 - 我的位置服务在 View Controller 类中完美运行,但是我意识到它在逻辑上位于早期的 View Controller 类中并将其移动到这里,但是从那时起位置服务完全被跳过,只运行序列。我已经检查了明显的内容( View Controller 列在文件检查器中)。非常感谢任何帮助,

import UIKit
import CoreLocation

var userLat = Double()
var userLong = Double()

class FirstViewController: UIViewController, CLLocationManagerDelegate {

//instantiate location manager
var locationManager = CLLocationManager()

//get location data
func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

//start updating location
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}

//get coordinates
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation

//check to see if user has allowed app to see location and use those lats if is the case
if CLLocationManager.locationServicesEnabled() {
userLat = userLocation.coordinate.latitude
userLong = userLocation.coordinate.longitude
}

//check if you got any data?
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")

//stop updating location
manager.stopUpdatingLocation()

}


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


//perform Seques
@IBAction func CheckLocation(_ sender: Any) {
//cecking if user location is enableded?
if CLLocationManager.locationServicesEnabled(){

performSegue(withIdentifier: "two", sender: self)

}

else{
performSegue(withIdentifier: "three", sender: self)
}

}

@IBAction func Locatiohn(_ sender: Any) {

performSegue(withIdentifier: "three", sender: self)
}




override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "sun.jpg")!)
// Do any additional setup after loading the view.

}


override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated )


}



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



// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
//override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.






}

最佳答案

我通过从以下代码中添加删除“func”并将其放入 viewDidLoad ....\

func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

//start updating location
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}

关于swift - 位置服务不会在新的 ViewController 类中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928514/

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