'MKMapView' / 'showsUserLocation = true' 导致模式转场错误
'MKMapView' / 'showsUserLocation = true' causing error with modal segue
我正在开发一个允许用户查看其当前位置的小型地图应用程序。我有执行此操作的相关代码,它似乎按预期工作:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate {
// CLlocation
var location: CLLocation!
let locationManager = CLLocationManager()
// Map variables
var searchController:UISearchController!
var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!
// IBOutlets
@IBOutlet weak var placesMap: MKMapView!
// Location function
func locationManager(manager: CLLocationManager, didUpdateqLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.placesMap.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error code: " + error.localizedDescription)
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.placesMap.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
但是,当我从弹出窗口(如下图所示)向 'About' 页面启动模式转场时,应用程序崩溃了:
我查看了终端给我的错误,这是
fatal error: unexpectedly found nil while unwrapping an Optional value
Xcode 将我指向 viewDidLoad 函数中的这一行:
self.placesMap.showsUserLocation = true
当我从我的代码中删除该特定行时,定位功能不再起作用,这是显而易见的。我已经检查了 MKMapView 的出口,它似乎是正确的。
我真的不知道如何避免这个错误,也不知道是什么导致了这个错误,所以非常感谢您的帮助。
基本上,某些东西正在使 placesMap
解除分配,然后 viewDidLoad
被调用。您可以尝试使 placesMap
可选而不是强制展开它,因此将 !
更改为 属性 上的 ?
然后将可选链接到 placesMap 的调用站点, 所以 self.placesMap.showsUserLocation
变成 self.placesMap?.showsUserLocation
而 self.placesMap.setRegion(region, animated: true)
变成 self.placesMap?.setRegion(region, animated: true)
我正在开发一个允许用户查看其当前位置的小型地图应用程序。我有执行此操作的相关代码,它似乎按预期工作:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate {
// CLlocation
var location: CLLocation!
let locationManager = CLLocationManager()
// Map variables
var searchController:UISearchController!
var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!
// IBOutlets
@IBOutlet weak var placesMap: MKMapView!
// Location function
func locationManager(manager: CLLocationManager, didUpdateqLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.placesMap.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error code: " + error.localizedDescription)
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.placesMap.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
但是,当我从弹出窗口(如下图所示)向 'About' 页面启动模式转场时,应用程序崩溃了:
我查看了终端给我的错误,这是
fatal error: unexpectedly found nil while unwrapping an Optional value
Xcode 将我指向 viewDidLoad 函数中的这一行:
self.placesMap.showsUserLocation = true
当我从我的代码中删除该特定行时,定位功能不再起作用,这是显而易见的。我已经检查了 MKMapView 的出口,它似乎是正确的。
我真的不知道如何避免这个错误,也不知道是什么导致了这个错误,所以非常感谢您的帮助。
基本上,某些东西正在使 placesMap
解除分配,然后 viewDidLoad
被调用。您可以尝试使 placesMap
可选而不是强制展开它,因此将 !
更改为 属性 上的 ?
然后将可选链接到 placesMap 的调用站点, 所以 self.placesMap.showsUserLocation
变成 self.placesMap?.showsUserLocation
而 self.placesMap.setRegion(region, animated: true)
变成 self.placesMap?.setRegion(region, animated: true)