在具有内容视图的tabbarcontroller和视图控制器之间传递数据

时间:2014-10-21 02:48:47

标签: ios uiviewcontroller uitabbarcontroller

我使用xib而不是故事板。

AppDelegate.m
ViewController1 *ViewController1 = [[ViewController1 alloc] init];
ViewController1.title = NSLocalizedString(@"1", @"1");
ViewController2 *ViewController2 = [[ViewController2 alloc] init];
ViewController2.title = NSLocalizedString(@"2", @"2");

BannerViewController *_bannerViewController1;
_bannerViewController1 = [[BannerViewController alloc] initWithContentViewController:ViewController1];

BannerViewController *_bannerViewController2;
_bannerViewController2 = [[BannerViewController alloc] initWithContentViewController:ViewController2];

_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = @[_bannerViewController1,_bannerViewController2];

self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];


ViewController1.m
#import "ViewController1.h"
#import "ViewController2.h"

ViewController2 *cvc = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
cvc.strReceiveValue= "Testing";

ViewController2.h
@property (strong, retain) NSString *strReceiveValue;
ViewController2.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"strMortgageAmount: %@", strMortgageAmount);

知道为什么我没有得到这个价值吗?我以为这可能与initWithContentViewController有关。

1 个答案:

答案 0 :(得分:0)

**

请参阅下面的更新

**

如果我正确理解了这个问题,你是在创建一个标签栏控制器(在AppDelegate?中)并希望将信息传递给与标签栏控制器相关的视图控制器?

如果是这样,请看一下:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController setDelegate:self];
UINavigationController *dashTVCnav = [[tabBarController viewControllers] objectAtIndex:0];

然后:

DashTVC *dashTVC = [[dashTVCnav viewControllers] objectAtIndex:0];
dashTVC.managedObjectContext = self.managedObjectContext;
dashTVC.uuid=uuid;

然后在DashTVC.h中:

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSString *uuid;

通过这个,您将managedObjectContect和uuid传递给dashTVC控制器。

我希望我明白你的要求......

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >

原始海报非常友好,可以给我他的示例代码。我将在这里发布部分内容,并为将来通过搜索找到此内容的人提供我的修复

有几个问题,但简而言之,没有任何内容传递给vc2:

1,没有从appDelegate传递任何内容

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect bounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:bounds];
self.window.backgroundColor = [UIColor whiteColor];

ViewController1 *vc1 = [[ViewController1 alloc] init];
vc1.title = NSLocalizedString(@"VC1", @"VC1");

ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.title = NSLocalizedString(@"VC2", @"VC2");

//---  I added this as a test
vc2.strReceiveValue=@"foo";

// and now foo shows up in the debug console when vc2 is fixed




_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = @[
    [[BannerViewController alloc] initWithContentViewController:vc1],
    [[BannerViewController alloc] initWithContentViewController:vc2],
];

self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;

}

接下来,有必要在viewController2.m

中修改viewWillAppear
- (void)viewWillAppear:(BOOL)animated {

// line below missing
[super viewWillAppear:animated];
NSLog(@"output: %@", strReceiveValue);

}

希望这有帮助,并享受IOS的开发!