swift iOS上未调用GMSMapViewDelegate函数

dy2hfwbg  于 2022-12-10  发布在  Swift
关注(0)|答案(2)|浏览(129)

我已经设置了implemented GMSMapViewDelegate,并将其设置为self,但仍然无法使任何委托函数工作。下面是我的代码示例:

class UserViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {

    var mapView: GMSMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if mapView == nil {
            mapView = GMSMapView()
        }

        mapView.delegate = self

    }

    func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
        print(position)
    }

    func mapView(mapView: GMSMapView, willMove gesture: Bool) {
        print(gesture)
    }

    func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
        print(position)
    }

所有mapView函数都没有打印任何内容。我已经检查了委托并知道它设置正确。我是否遗漏了什么?

3wabscal

3wabscal1#

所以我想在
if mapView == nil { mapView = GMSMapView() }
我在后面的函数中设置了实际的边界
mapView = GMSMapView.mapWithFrame(CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height), camera: camera)和一旦我把mapView.delegate = self之后,它的工作!

euoag5mw

euoag5mw2#

对我来说,修复不是从UIView的故事板连接检查器设置委托。即使我已经通过代码设置了它,但它不起作用。
转到情节提要,选择您的UIView,然后在身份检查器中添加类名GMSMapView
See Screeshot 1
然后点击连接检查器,在出口你会看到委派。拖动它到你的视图控制器。
see Screeshot 2

相关问题