6

我在我的项目中使用了一个选项卡栏控制器,其中 FirstViewController 有一个 Mapbox 地图视图,而 SecondViewController 有按钮,当按下这些按钮时,可以将一个图块层添加到地图视图中。这是我尝试过的。它会创建错误***在 SecondViewController.m 中使用未声明的标识符“_mapView”

//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]; 

 }
}

地图调用是正确的,因为我已经在一个仅使用一个视图控制器的项目上对其进行了测试。现在我在标签栏控制器上尝试它,我得到了这个错误。

我的问题是

1.如何让FirstViewController中的mapView响应SecondViewController中的调用?2.这个可以吗?我已经导入了类文件,认为这会打开两者之间的通信,但我遇到了这个错误。

4

3 回答 3

16

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.

于 2013-09-07T14:08:45.943 回答
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.

于 2013-09-07T14:08:18.650 回答
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];

}
于 2013-09-07T16:30:28.873 回答