关闭视图控制器时应用程序崩溃
App crashes when dismissing view controller
我有一个简单的 ViewController 可以显示我当前的位置坐标。
一切正常,但当我关闭 ViewController 时,应用程序崩溃,没有任何特定的错误日志。
class 代码如下:
import UIKit
class LocationViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UIPopoverPresentationControllerDelegate {
// General objects
@IBOutlet var closeButton: UIButton!
@IBOutlet var latitudeLabel: UILabel!
@IBOutlet var longitudeLabel: UILabel!
@IBOutlet var infoButton: UIButton!
// Global variables
var location: CLLocationManager? = CLLocationManager()
var geocoder = CLGeocoder();
var placemark = CLPlacemark();
var hasPin: Bool = false;
override func viewDidLoad() {
super.viewDidLoad()
// Ask for Authorisation from the User.
location?.requestAlwaysAuthorization();
// For use in foreground
location?.requestWhenInUseAuthorization();
getCurrentLocation();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func closeButton(_ sender: AnyObject) {
self.dismiss(animated: true, completion: {
print("dismissing locationViewController");
self.location = nil;
});
}
@IBAction func infoButton(_ sender: AnyObject) {
// TODO
}
// MARK: - General functions
func getCurrentLocation() -> Void {
if (CLLocationManager.locationServicesEnabled()) {
location?.delegate = self;
location?.desiredAccuracy = kCLLocationAccuracyBest;
location?.startUpdatingLocation();
}
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("ERROR = \(error)");
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Gets the user coordinates
let locValue:CLLocationCoordinate2D = manager.location!.coordinate;
USER_LATITUDE = locValue.latitude;
USER_LONGITUDE = locValue.longitude;
longitudeLabel.text = "\(USER_LONGITUDE)";
latitudeLabel.text = "\(USER_LATITUDE)";
location?.stopUpdatingLocation()
}
有人知道为什么会这样吗?
没有提示错误日志,这让我更加困惑。
首先我认为我必须将位置变量设置为可选,然后在我关闭 VC 时将其设置为 nil 但崩溃仍在发生。
Crashlytics 说应用程序在 LocationViewController 行 0 内崩溃,这实际上很奇怪。
我称之为 ViewController,从一个按钮点击另一个 VC 像这样:
@IBAction func locationButton(_ sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController = storyboard.instantiateViewController(withIdentifier: "locationVC");
self.present(viewController, animated: true, completion: nil);
}
我在 iOS 10.
上使用带有最新 Xcode Beta 版本的 Swift3
- 谢谢
替换为:
var location: CLLocationManager? = CLLocationManager()
有了这个:
let location = CLLocationManager()
根据需要更改所有代码(这不再是 Optional,因此没有什么可解包的)并删除试图将其设置为 nil
.
的行
如果您担心位置管理器可能会在您关闭时试图获取您的位置,请执行 viewWillDisappear
并告诉它停止更新。
您需要在Info.plist中添加隐私条目,并请求授权使用定位服务。可以在这里找到一个很好的概述:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
我有一个简单的 ViewController 可以显示我当前的位置坐标。
一切正常,但当我关闭 ViewController 时,应用程序崩溃,没有任何特定的错误日志。
class 代码如下:
import UIKit
class LocationViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UIPopoverPresentationControllerDelegate {
// General objects
@IBOutlet var closeButton: UIButton!
@IBOutlet var latitudeLabel: UILabel!
@IBOutlet var longitudeLabel: UILabel!
@IBOutlet var infoButton: UIButton!
// Global variables
var location: CLLocationManager? = CLLocationManager()
var geocoder = CLGeocoder();
var placemark = CLPlacemark();
var hasPin: Bool = false;
override func viewDidLoad() {
super.viewDidLoad()
// Ask for Authorisation from the User.
location?.requestAlwaysAuthorization();
// For use in foreground
location?.requestWhenInUseAuthorization();
getCurrentLocation();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func closeButton(_ sender: AnyObject) {
self.dismiss(animated: true, completion: {
print("dismissing locationViewController");
self.location = nil;
});
}
@IBAction func infoButton(_ sender: AnyObject) {
// TODO
}
// MARK: - General functions
func getCurrentLocation() -> Void {
if (CLLocationManager.locationServicesEnabled()) {
location?.delegate = self;
location?.desiredAccuracy = kCLLocationAccuracyBest;
location?.startUpdatingLocation();
}
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("ERROR = \(error)");
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Gets the user coordinates
let locValue:CLLocationCoordinate2D = manager.location!.coordinate;
USER_LATITUDE = locValue.latitude;
USER_LONGITUDE = locValue.longitude;
longitudeLabel.text = "\(USER_LONGITUDE)";
latitudeLabel.text = "\(USER_LATITUDE)";
location?.stopUpdatingLocation()
}
有人知道为什么会这样吗?
没有提示错误日志,这让我更加困惑。
首先我认为我必须将位置变量设置为可选,然后在我关闭 VC 时将其设置为 nil 但崩溃仍在发生。
Crashlytics 说应用程序在 LocationViewController 行 0 内崩溃,这实际上很奇怪。
我称之为 ViewController,从一个按钮点击另一个 VC 像这样:
@IBAction func locationButton(_ sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController = storyboard.instantiateViewController(withIdentifier: "locationVC");
self.present(viewController, animated: true, completion: nil);
}
我在 iOS 10.
上使用带有最新 Xcode Beta 版本的 Swift3- 谢谢
替换为:
var location: CLLocationManager? = CLLocationManager()
有了这个:
let location = CLLocationManager()
根据需要更改所有代码(这不再是 Optional,因此没有什么可解包的)并删除试图将其设置为 nil
.
如果您担心位置管理器可能会在您关闭时试图获取您的位置,请执行 viewWillDisappear
并告诉它停止更新。
您需要在Info.plist中添加隐私条目,并请求授权使用定位服务。可以在这里找到一个很好的概述:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/