Reputation: 31
I'm doing an app that I input one address in one ViewController and this address goes to the MapView in other ViewController and add an annotation. But when I add the address when return to the mapview the app crashes and show me that my mapview.delegate is nil. Here is my code for the address input:
@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()
}
})
}
}
}
Here is my mapview:
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)
}
}
}
And here is the error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Thanks all.
Upvotes: 0
Views: 282