Search code examples
iosuitableviewuinavigationcontrolleruisplitviewcontrollergmsmapview

Using didSelectRowAtIndexPath with UISplitViewController and Google Maps


I am using a UITableViewController inside of a UINavigationController for my Master and I'm using a UIViewController implementing the GMSMapViewDelegate inside of a UINavigationController for my detail side to display a google map. Currently the table view and the Google map are displaying in the UISplitViewController fine.

I am a beginner who recently finished reading Programming in Objective C and Big Nerd Ranch's guide for IOS 7. I can't figure out how to use the didSelectRowAtIndexPath method to change the camera position with the map. I know how to change the camera position, I've wrote NSLog calls to test whether my app was responding when tapping a particular row, but I can't figure out how to connect the two controllers. I thought about trying to make the controller holding the mapview a delegate for the UITableView, but I am confused as to how to connect the two. What options do I have to carry something like that out.

This is what my appdelegate file looks like.

...
mapviewController *mvc = [[mapViewController alloc]init];
locationTableController *ltc = [[locationTableController alloc]init];

UISplitViewController *svc = [[UISplitViewController alloc]init];
UINavigationController *sideNav = [[UINavigationController alloc]initWithRootViewController:ltc];
UINavigationController *mapNav = [[UINavigationController alloc]initWithRootViewController:mvc];
svc.delegate = mapNav;
svc.viewControllers = @[sideNav,mapNav];
....

Solution

  • I would keep your locationTableController as delegate and datasource for the table. You can use self.splitViewController to access the splitViewController, your mapNav is then at viewControllers[1] and your mapviewcontroller is rootViewController of mapNav. If you implement a changeCameraPosition method in your mapviewcontroller, you can call this from within didSelectRowAtIndexPath. So in didSelectRowAtIndexPath:

    UISplitViewController *svc = self.splitViewController;
    UINavigationController *mapNav = svc.viewControllers[1];
    mapViewController *mvc = (mapViewController *)mapNav.rootViewController;
    [mvc changeCameraPosition];
    

    You may need to import the relevant .h files if not already done. Personally I would add some properties to the splitViewController to speed up accessing the other view controllers.