在标签栏控制器项目中的2个ViewControllers之间进行通信

use*_*075 5 objective-c uitabbarcontroller tabbarcontroller ios ios7

我在我的项目中使用Tab Bar Controller,其中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]; 

 }
}
Run Code Online (Sandbox Code Playgroud)

地图调用是正确的,因为我在仅使用一个视图控制器的项目上测试了它.现在我正在Tab Bar Controller上尝试它,我得到了这个错误.

我的问题是

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

Mru*_*nal 16

使用tabbar控制器,您可以在所有关联的视图控制器上获取阵列.

您可以在此处找到更多详细信息:UITabbarController - viewControllers属性

例如 :

在tabbar中,如果我们有两个视图控制器,比如VC1和VC2,那么我们可以使用下面的代码片段来获得这些参考中的任何一个.

VC2类实现(VC2.m)中访问VC1引用:

VC1 *myVC1ref = (VC1 *)[self.tabBarController.viewControllers objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

现在我们可以使用VC1类的公共属性和方法,它将提供tabbar加载的相同引用.

希望这可以帮助.