Jake T.
Jake T.

Reputation: 4378

iOS Google Maps API - send a GMSMapview from one view controller in another

I'm relatively new to iOS dev and completely new to using the Google Maps API.

I have two separate views that I want to have a mapview. The first one is a scene that allows you to find a location on the map, enter in an address, or use your current location. In the next scene, I want to use the same location that the user input from the previous scene, but rather than the regular map I want to use the satellite view, and there will be some additional tools to manipulate the map to serve the purposes of my app.

I need to have two separate scenes, as the interfaces are very different, but I'm having trouble sending the information from one mapview to the other. Here's a few methods that I've tried(two are commented out):

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showServiceArea"]) {
    ServiceAreaViewController *destViewController = segue.destinationViewController;
    destViewController.camera = [GMSCameraPosition cameraWithLatitude:curLocation.location.coordinate.latitude longitude:curLocation.location.coordinate.longitude zoom:17]; //Setting up a camera in the next scene using the curLocation coordinates from the current view
    //destViewController.view = _mapView;  //Setting the next view controller's view to the current mapView and changing the mapType to satellite
    //_mapView.mapType = kGMSTypeSatellite;
    //destViewController.serviceAreaMapView = _mapView;  //copying the current mapview to a mapview on the next scene
    //destViewController.serviceAreaMapView.mapType = kGMSTypeSatellite;
}

}

Every method that I've tried has thrown an exception and caused my app to crash. Here's the output from the crash of the above code:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setCamera:]: unrecognized selector sent to instance 0x17e73eb0'

For the first two commented out methods, I figured that by the time the next scene was loaded, the data I had assigned to the next view was being deleted. I'm not sure what is wrong with my current code.

Could anybody point me in the right direction to produce a mapView in a following scene using the current scene's data? Thanks.

Upvotes: 1

Views: 987

Answers (1)

ztan
ztan

Reputation: 6921

I dont think you should pass UI between two controllers. You should only pass the non-UI data to your second controllers. Especially your UI data are probably weak, which would probably become nil. In addition, prepareForSegue happens before your second controller's view is loaded, whatever UI data you pass to the second controller will not be set.

The correct way to pass data between is to pass non-UI data. For example, if you have a UITextView in your first controller, you should not do secondController.textView = firstController.textView;, instead, you should pass the non-UI attributes. So you need to make a NSString *myText; in your controllers, then you can do secondController.myText = self.myText; in your first controller's prepareForSegue method. When your second controller's view is loaded (your UITextView will be loaded) in viewWillLoad() or viewDidLoaded() method, you can set your UITextView with the myText data in your second controller: self.textView = myText

In your case, you should not pass the mapView from your first controller to the second controller, you should declared variables to hold your mapView in your second controller, then you can pass the non-UI mapView attribute data to the strong variables in your second controller.

Upvotes: 3

Related Questions