Search code examples
iosswiftgoogle-mapsgoogle-maps-sdk-iosswift4

Is it possible to add the same view to two different view controllers?


I get the following error message:

A view can only be associated with at most one view controller at a time!

Is it possible to add the same view to two different view controllers?

I want to add one instance of a Google Map to a Tab View Controller.

I'm using swift4 with the storyboards.


Solution

  • If it were a typical view then you should create an XIB and add a new instance of it to each view. Since you're using GMSMapView from the Google Maps SDK, and you want to have the same instance across multiple tabs the approach will be different.

    You can either:

    1. New Instance, Same Parameters: Create a new instance in each tab with the parameters set to be the same as the other tabs.
    2. Move Around the Single Instance: Create an instance of GMSMapView. Store it in a shared property. Use addSubview() to move it around. Basically use addSubview() in the new tab to remove it from the old one and add it to the new one.