如何仅保留一个Google Maps for iOS选项卡视图控制器实例

时间:2017-10-23 13:37:19

标签: ios swift google-maps google-maps-sdk-ios

如何仅为iOS标签视图控制器使用一个Google地图实例?

我在标签视图控制器中有不同的标签,有些则使用Google Maps iOS SDK。出于内存原因,我只想保留mapView的一个实例。

我在Swift 4中编写代码。

1 个答案:

答案 0 :(得分:0)

我在之前的项目中也做过类似的问题。我有地图屏幕和详细的地图屏幕。我重复使用GMSMapView两个屏幕。共同经理在过滤/退出ViewController时过滤了地图的数据。因此,您可以检测TabViewController中新选项卡的选择,并将更改应用于mapView。这将减轻你的内存加载,特别是如果你在mapView中使用了很多符号。

使用示例代码段编辑:

以下是示例代码段

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if self.mapView?.superview != self.mapViewContainer {
            self.mapView?.placeFullScreenInView(self.mapViewContainer)

            ... // some VC logic handling appearance of the mapView and business logic here
        }
}`

更多信息: 的观点:

@IBOutlet private weak var mapViewContainer: UIView! // UIView - container of the GMSMapView
weak var mapView: GMSMapView! // the shared GMSMapView between the controllers; In my case I passed this view in prepareForSegue because it was the easier way: VC -> (push) Detailed VC. You might need separate class managing this transfer.

自定义方法

placeFullScreenInView - pretty self explanatory - placing the mapView as a subview with 4 constraints to the top, right, bot, left (0) in mapViewContainer.

在我的基础第一个VC中,我也有与mapView / mapViewContainer相同的逻辑:

override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        self.map?.removeFromSuperview()
        //... logic for nullifying not needed data
    }