【问题标题】:Unrecognized selector sent to instance.. 'NSInvalidArgumentException' for MapView delegate无法识别的选择器发送到实例.. MapView 委托的“NSInvalidArgumentException”
【发布时间】:2018-02-25 22:13:07
【问题描述】:

Swift 4/谷歌地图。

我得到的崩溃是..

由于未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'-[UIView setDelegate:]: 无法识别的选择器发送到实例 0x7fa1eb50d7e0'

是的,我知道这有很多 个线程,但是浏览它们并没有帮助我。我在有嫌疑的地方已经调试够了,但不知道具体如何解决。

Here's the git repo

一些细节.. 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


【解决方案1】:

查看您的代码和情节提要后。 我可以看到你已经连接了

@IBOutlet var mapView: GMSMapView!

到父视图而不是实际的 GMSMapView 所以请重新检查 IBOutlet 连接。

【讨论】:

  • 是的,我以前犯过那个严重的错误!谢谢你的收获
  • 欢迎它也发生在我身上..随时为答案投票
【解决方案2】:

我有类似的问题,但问题是我没有从目标继承模块

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 2012-07-16
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2012-02-21
    相关资源
    最近更新 更多