我正在做一个应用程序,我在一个ViewController中输入一个地址,该地址转到另一个ViewController中的MapView并添加注释。 但是,当我在返回mapview时添加地址时,应用崩溃了,并显示了我的mapview.delegate为零。 这是我输入地址的代码:
@IBAction func createButtonPressed(_sender: Any) {
guard let address = addresTxtFd.text else { return }
guard let date = dateTxtFd.text else { return }
let geocoder = CLGeocoder()
geocoder.geocodeAddressString((address)) { (placemarks, error) in
if error == nil {
if let placemark = placemarks?[0] {
let location = placemark.location!
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
self.locatinService.setLocation(address: address, date: date, latidude: latitude, longitude: longitude, completion: { (success) in
if success {
let map = MapVC()
map.modalPresentationStyle = .custom
self.present(map, animated: true, completion: nil)
// LocationServices().dropPin()
}
})
}
}
}
这是我的地图视图:
class MapVC: UIViewController {
// Outlets
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var newGSButton: UIButton!
// Variables
var locationManager = CLLocationManager()
var locationAuthorization = CLLocationManager.authorizationStatus()
var regionRadius: Double = 1000
let annotations = MKPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager.delegate = self
configureCurrentLocation()
}
@IBAction func centerUserLocation(_ sender: Any) {
centerUserCurrentLocation()
}
@IBAction func newGSButtonPressed(_ sender: Any) {
if AuthService.instance.isLoggedIn
{
let newGs = NewGarageSaleVC()
newGs.modalPresentationStyle = .custom
present(newGs, animated: true, completion: nil)
} else {
performSegue(withIdentifier: TO_LOGIN, sender: nil)
}
}
}
这是错误:
线程1:致命错误:展开包装时意外发现nil 可选值
谢谢。