Communicating between 2 ViewControllers in Tab Bar Controller Project
Asked Answered
H

3

6

I'm using a Tab Bar Controller in my project where the FirstViewController has a Mapbox map view and the SecondViewController has buttons that when pressed add a tile layer to the map view. Here's what I've tried. It creates error ***Use of undeclared identifier '_mapView' in the 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 map calls are correct because I've tested it on a project that only uses one view controller. Now that I'm trying it on a Tab Bar Controller I get this error.

My question is

1.how do I get mapView in the FirstViewController to respond to calls in the SecondViewController? 2.Can this be done? I've imported the class files thinking this would open communication between the two but I'm stuck with this error.

Hyperkeratosis answered 7/9, 2013 at 13:55 Comment(0)
J
17

Using tabbar controller you can get array on all associated view controllers.

You can find more details here: UITabbarController - viewControllers property

For example :

In a tabbar, if we have two view controllers, let say VC1 and VC2, then we can have any of these reference by using below code snippet.

Accessing VC1 reference in VC2 class implementation (VC2.m):

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

Now we can use public properties and methods of VC1 class and it will give the same reference which tabbar has loaded.

Hope this helps.

Janka answered 7/9, 2013 at 14:8 Comment(2)
You can do it like this way, but I don't think it's a good way maybe you can use notifications or singleton to implement what you wantStipule
[self.tabbarController.viewControllers firstObject] It's more secureBiffin
C
1

You basically add view controller to UITabBarController. So, if you need to access a UIViewControler in a specific tab, you need to query the UITabBarController. The following answered SO question might help you

any code example of how access viewcontroller from uitabbarcontroller?

Once you get hold of the view controller, you can pass all the data you want.

Cherida answered 7/9, 2013 at 14:8 Comment(2)
I went with Mrunal's answer because he provided the code I needed to access a different view controller but I couldn't have done it without your input too. Thank you.Hyperkeratosis
No problem, glad you solved the problem. Good luck with your app.Cherida
H
1

Thanks to Mrunal and Naz Mir.

I added a UITabBarController Class file and assigned it to my TabBarController. I then NSLog the array description of view controllers of the TabBarController.

//TabBarController.h

@property (strong, nonatomic) NSArray *array;

//TabBarController.m

- (void)viewDidLoad
{

NSArray *array = self.viewControllers;

NSLog(@"View Controllers = %@", [array description]);

}

I then imported the FirstViewController.h to SecondViewController.h and in SecondViewController.m I wrote...

//SecondViewController.m

- (IBAction)stationingLayerButton:(id)sender {


FirstViewController *FVC = [self.tabBarController.viewControllers objectAtIndex:0];


[[FVC mapView] addTileSource:stationingMap atIndex:2];

}
Hyperkeratosis answered 7/9, 2013 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.