3

I currently have three view controllers: ViewController1 ViewController2 and ViewController3. I'm trying to pass data between ViewController1 and ViewController2 and between ViewController2 and ViewController3.

For example, to pass data user holds from ViewController1 to ViewController2 I use:

ViewController2 *vc2 = [[ViewController2 alloc]init];
vc2.user = _user;    

[self.navigationController pushViewController:vc2 animated:YES];

but if I try to pass user from ViewController2 and ViewController3, I can't use the above method...I just get a black view controller appear on screen.

Instead, I tried this and it works:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
ViewController3 *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"vc3Storyboard"];

vc3.user = _user;

[self.navigationController pushViewController:vc3 animated:YES]; 

Why can't i use the same method i used for vc1 -> vc2?

ViewController3 *vc3 = [[ViewController3 alloc]init];
vc3.user = _user;

[self.navigationController pushViewController:vc3 animated:YES];
  • Is it possible you made a typo and tried to push a view controller that was already on the navigation stack? This would cause the behavior you describe. – Aaron Brager Jan 01 '14 at 04:00
  • Definitely seems to be a typo. Can you post that code also. – Evol Gate Jan 01 '14 at 04:01
  • @AaronBrager No I didn't! i checked hundreds of times for any errors i mustve done but none... – user3150273 Jan 01 '14 at 04:01
  • Are you sure you instantiated ViewController3 properly (I mean, separate from the method you tried in your second code block)? Code you post the block of code showing that initial instantiation? – Lyndsey Scott Jan 01 '14 at 04:11
  • @LyndseyScott wait, how do i initialize ViewController3 properly? I tried this: `ViewController3 *vc3 = [[ViewController3 alloc]init];` – user3150273 Jan 01 '14 at 04:16
  • @LyndseyScott yeah I could but I'm just wondering why this is not working.. – user3150273 Jan 01 '14 at 04:25
  • To be honest, I'm still trying to figure out why it *is* working for you in the first code snippet when you're going from VC1 to VC2, though it makes complete sense that your code in the second snippet works… Here's a popular stack overflow post about the subject: http://stackoverflow.com/a/8348527/2274694 . Your VC2 isn't blank is it? – Lyndsey Scott Jan 01 '14 at 04:31
  • @LyndseyScott i shall take a look and no its not! – user3150273 Jan 01 '14 at 04:45

3 Answers3

2

THe reason why you are getting a black screen is because of the method you are using to instantiate it.

 ViewController2 *vc2 = [[ViewController2 alloc]init];

This successfully creates a view controller, but alas, it has no view (you didn't give it one). The navigation controller will add the view-less view controller to its stack, animating the previous controller out. Since the default background color of the nav controller is black and the top view controller is nonexistent, then you are experiencing exactly what should appear: nothing.

The reason why your second method works is because the storyboard is instantiating the view controller, but this time it has an actual view:

ViewController3 *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"vc3Storyboard"];
Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
0

First of all you have to initiate your viewController as @Wayne Hartman said..

ViewController2 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
vc.user = _user;
[[self navigationController] pushViewController:vc animated:YES];

Now in your ViewController2 and ViewController3 variable "user" must have @Property and @synthesize

@property(nonatomic, copy) NSString *user;
@synthesize user;

And now you can navigate in the ViewController3 same as ViewController2

ViewController3 * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController3"];
vc.user = user;
[[self navigationController] pushViewController:vc animated:YES];

You can find the ViewController Identifier from the Storyboard > Identity inspector

enter image description here

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
0

You can use this code

        NSMutableArray* array = [self.navigationController viewControllers];
        FirstViewController* viewFirst = [array objectAtIndex:0];
        // Get the third / second or any view controller using its order of insertion / push
        // If you know th order then
        ThirdViewController* thirdView = [array objectAtIndex:2];
        thirdView.view.backgroundColor = viewFirst.view.backgroundColor;

If that doesn't work

You can use NSNotificationfor you task

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Kumar C
  • 525
  • 6
  • 21