0

I am using xib instead of storyboard.

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);

Any idea why I failed to get the value? I was thinking this could be related to initWithContentViewController.

Kenny
  • 21
  • 4

1 Answers1

0

**

See update below

**

If I understand the question correctly, you are creating a tab bar controller (in AppDelegate?) and wish to pass information to the view controllers associated with the tab bar controller?

If so, take a look at this:

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

Then:

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

Then in DashTVC.h:

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

With this, you pass the managedObjectContect and the uuid to the dashTVC controller.

I hope I understood what you are asking...

+++++++++++++++ UPDATE +++++++++++++++++++++

The original poster was kind enough to give me his sample code. I will post parts of it here with my fix for those who find this through searches in the future

There were several problems, but in short, nothing was being passed to vc2:

1st, nothing was being passed from the 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;

}

Next, it was necessary to modify viewWillAppear in viewController2.m

- (void)viewWillAppear:(BOOL)animated {

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

}

Hope this helps, and enjoy developing for IOS!

jmf1205
  • 437
  • 6
  • 23
  • This is a good answer to review as well http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – jmf1205 Oct 21 '14 at 04:44
  • I have updated the question, I think it should be cleared now to explain the problem ... hopefully. – Kenny Oct 21 '14 at 09:47
  • First, you have obviously edited out a bunch of code, which is fine although there could be other problems, but where did this come from? NSLog(@"strMortgageAmount: %@", strMortgageAmount); Your NSString property is *strReceiveValue; – jmf1205 Oct 21 '14 at 10:57
  • It was a typo :) NSLog(@"strReceiveValue: %@", strReceiveValue); – Kenny Oct 21 '14 at 12:16
  • Let me create the sample and share it out in dropbox .. I am new to iOS but I really enjoy playing with it ... your help is appreciated .. – Kenny Oct 21 '14 at 12:19
  • Did this fix things for you? – jmf1205 Oct 22 '14 at 11:35
  • Here is my storyboard: TabBarController -> Relationship Segue -> ViewController (ClassBanner) -> Embed -> ViewController (ClassA) -> Relationship Segue -> ViewController (ClassBanner) -> Embed -> ViewController (ClassB) I have a TabBarDelegate in ClassBanner. I need to send the value from ClassA to ClassB. Any idea how to do it? Have you seen this design before? – Kenny Oct 22 '14 at 15:08
  • One thing you can try is using a singleton. There are quite a few references out there with example code. – jmf1205 Oct 25 '14 at 04:25