【发布时间】:2018-02-25 22:13:07
【问题描述】:
Swift 4/谷歌地图。
我得到的崩溃是..
由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'-[UIView setDelegate:]: 无法识别的选择器发送到实例 0x7fa1eb50d7e0'
是的,我知道这有很多 个线程,但是浏览它们并没有帮助我。我在有嫌疑的地方已经调试够了,但不知道具体如何解决。
一些细节.. MapViewController 有三个 UIView。
- 地图视图控制器
- 主视图(控制器自带的默认) -- 菜单(主视图的子视图) -- 地图(主视图的子视图)
我相信我已将崩溃范围缩小到声明
self.mapView.delegate = self
这是 MapViewController 的大部分代码。
import UIKit
import GoogleMaps
import FirebaseDatabase
class MapViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var mapView: GMSMapView!
var locationOfInterestMarker = GMSMarker()
var userMarker = GMSMarker()
var locationManager = CLLocationManager()
let zoom:Float = 15
let locationOfInterestImage:String = "hhouseicon"
let userMarkerImage:String = "witchicon"
override func viewDidLoad() {
super.viewDidLoad()
self.getLocations()
self.locationManager.delegate = self
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startMonitoringSignificantLocationChanges()
/**
The next line causes crash. I believe I need to set the delegate to something other than self. Set the delegate to the View named Map. However I'm not sure about the actual code for that.
**/
self.mapView.delegate = self //Crashes
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
self.locationManager.startUpdatingLocation()
self.mapView.isMyLocationEnabled = true
self.mapView.settings.myLocationButton = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
self.locationManager.stopUpdatingLocation()
self.mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
self.placeMarker(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude, marker: self.userMarker, imageName: userMarkerImage)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if let destination = segue.destination as? LocationDetialViewController {
destination.coordinate = locationManager.location?.coordinate
} else {
return
}
} . . .
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
performSegue(withIdentifier: "locationDetailSegue", sender: self)
return false
}
}//MapViewController
【问题讨论】:
-
您没有 GMSMapView 的 IBOutlet 对象。
标签: ios swift google-maps