The relationship between 2 ViewControllers in a bar-bar panel project

I use the tab bar controller in my project, where FirstViewController looks like a Mapbox map, and SecondViewController has buttons that, when clicked, add a tile layer to the map view. Here is what I have tried. It creates an error *** Using undeclared identifier "_mapView" in SecondViewController.m.

//FirstViewController.h #import <UIKit/UIKit.h> #import <MapBox/MapBox.h> #import "SecondViewController.h" #import "SecondViewController.m" @interface FirstViewController : UIViewController @property (strong, nonatomic) IBOutlet RMMapView *mapView; @end //SecondViewController.h #import <UIKit/UIKit.h> #import <MapBox/MapBox.h> #import "FirstViewController.h" #import "FirstViewController.m" @interface SecondViewController : UIViewController - (IBAction)stationingLayerButton:(id)sender; @end //SecondViewController.m - (IBAction)stationingLayerButton:(id)sender { RMMBTilesSource *stationingMap = [[RMMBTilesSource alloc] initWithTileSetURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Stationing20" ofType:@"mbtiles"]]]; [_mapView addTileSource:stationingMap atIndex:2]; } } 

The card calls are correct because I tested them in a project that uses only one view controller. Now, when I try to use it on the input panel controller, I get this error.

My question

1. How can I get mapView in FirstViewController to answer calls in SecondViewController? 2. Could this be done? I imported the class files, thinking that this would open the connection between them, but I was stuck with this error.

+4
source share
3 answers

Using the table controller, you can get an array on all related view controllers.

Here you can find more detailed information: UITabbarController - property viewControllers

For instance:

On the tab, if we have two view controllers, say VC1 and VC2 , then we can have any of these links using the code snippet below.

Access to VC1 in VC2 : (VC2.m) :

 VC1 *myVC1ref = (VC1 *)[self.tabBarController.viewControllers objectAtIndex:0]; 

Now we can use the public properties and methods of the VC1 class, and it will provide the same link that the tab loaded.

Hope this helps.

+16
source

You basically add a view controller to the UITabBarController . So, if you need to access the UIViewControler in a separate tab, you need to request the UITabBarController . The answer to the following SO question can help you

any sample code like access view manager from uitabbarcontroller?

Once you have mastered the view controller, you can transfer all the data you need.

+1
source

Thanks to Munnal and Naz Mir.

I added the class file UITabBarController and assigned it to my TabBarController. Then I NSLog describes the description of an array of controllers of the form TabBarController.

 //TabBarController.h @property (strong, nonatomic) NSArray *array; //TabBarController.m - (void)viewDidLoad { NSArray *array = self.viewControllers; NSLog(@"View Controllers = %@", [array description]); } 

Then I imported FirstViewController.h into SecondViewController.h and into SecondViewController.m I wrote ...

 //SecondViewController.m - (IBAction)stationingLayerButton:(id)sender { FirstViewController *FVC = [self.tabBarController.viewControllers objectAtIndex:0]; [[FVC mapView] addTileSource:stationingMap atIndex:2]; } 
+1
source

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


All Articles