Access MKMapView through the tab bar

I have a tab app, and the first tab has MKMapView . What I want to do is somewhere else in the application, switch the active tab to mapview and set the display area based on the data in the previous view (the one in which the button switches to display).

I tried:

 [self.tabBarController setSelectedView:0]; UIMapViewController *mapView = [self.tabBarController.viewControllers objectAtIndex:0]; [mapView displayBookmarkAnnotation:bookmark]; 

It just crashes the application, which cannot find the method I created. I do not think that I have chosen the best way to implement this, but I am really not sure how I should do it.

[Update] Casting the controller returned by tabBarController had no effect.

[solvable] I'm trying to use a UINavigationController for my mapView

 [self.tabBarController setSelectedView:0]; UINavigationController *navController = [self.tabBarController.viewControllers objectAtIndex:0]; //if the tab has other views open, return to mapView [navController popToRootViewControllerAnimated:YES]; UIMapViewController *mapView = (UIMapViewController *)[navController visibleViewController]; [mapView customMessage:object]; 
+4
source share
3 answers

Are you sure that the main view controller for this tab is not a UINavigationController? If so, you can get a root view controller for what should be your UIMapViewController.

It would be nice to put a direct link in AppDelegate, although if you are going to call it from another place.

+1
source

What you want to do is create a subclass or category of UITabBarController that

  • for NotificationCenter events that you define
  • handles events using the new selector. I usually use a naming convention for them.

When the event passes through you, you will set the selectedIndex value.

0
source

Why not get directions through AppDelegate? AppDelegate can have UITabBarController and MKMapView (both wired through the interface builder). The UIButton handler UIButton also be in AppDelegate so that it can call -[UITabBarController setSelectedView:] and -[MKMapView setRegion:] .

0
source

Source: https://habr.com/ru/post/1299394/


All Articles