-2

I have three ViewController A, B, and C.

I want to pass a data from A to C. But when user uses my app, he has to pass by B ViewController before C ViewController. And the segue identifier is between A and B.

ViewController A and C aren't linked in Storyboard. May be it's the problem...

I try this but Xcode doesn't recognize variable ndj2 from ViewController C.

I put the correct #import ViewControllerC.h

I already try to pass date between A and B, and it's working. It's to avoid to do two prepareForSegue (one between ViewController A and ViewController B / and another between ViewController B and ViewController C)

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

   ViewControllerA *transferViewController = segue.destinationViewController;

       NSLog(@"prepareForSegue: %@", segue.identifier);
       if([segue.identifier isEqualToString:@"quelnom"]) // identifier between A and B
      {

           transferViewController.ndj2 = ndj;
      // ndj2 is variable present in ViewController C
      }
   };

The real problem is ndj2 is non recognize , and i declared it properly. It's the Viewcontroller the problem but i don't understand why...

user2971617
  • 63
  • 1
  • 9
  • You did not tell what exactly does not work. I'm assuming that you did not get any error message? – plu Jan 19 '14 at 10:54
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – icodebuster Jan 20 '14 at 16:36

3 Answers3

0

in C ViewController you ndj2 initialized as @property and ViewControllerC *transferViewController = segue.destinationViewController;

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
       NSLog(@"prepareForSegue: %@", segue.identifier);
       if([segue.identifier isEqualToString:@"quelnom"]) // identifier between A and B
      {
           ViewControllerC *transferViewController = segue.destinationViewController;
           transferViewController.ndj2 = ndj;
      // ndj2 is variable present in ViewController C
      }
};
R00We
  • 1,931
  • 18
  • 21
0

If ndj2 is declared as a property in both ViewControllerB and ViewControllerC, and these two ViewControllers are also connected by a segue, then you just need to initialize ViewControllerC.ndj2 in prepareForSegue method of ViewControllerB just like you did in ViewControllerA.

Now you initialize only ViewControllerB.ndj2, but it is not passed to ViewControllerC automatically.

FreeNickname
  • 7,398
  • 2
  • 30
  • 60
0

may be you need to look into how to use singleton objects to transfer data between views in a neat way.. for example i use this

for propertyManager.h

#import <Foundation/Foundation.h>

@interface PropertyManager : NSObject{

}
+ (PropertyManager*) sharedPropertyManager;


@property(nonatomic,retain)NSString* chapterId;
@property(nonatomic,retain)NSString* chapterName;
@property(nonatomic,retain)NSString * clicked;

@end

and for .m file

#import "PropertyManager.h"

@implementation PropertyManager
static PropertyManager* _sharedPropertyManager = nil;

+ (PropertyManager*) sharedPropertyManager{
    @synchronized([PropertyManager class]){

        if (!_sharedPropertyManager) {
            [[self alloc] init];
        }
        return _sharedPropertyManager;
    }
    return nil;
}

+ (id) alloc{
    @synchronized([PropertyManager class]){
        NSAssert(_sharedPropertyManager==nil,@"Attempted to allocate a second instance of the PropertyManager");
        _sharedPropertyManager = [super alloc];
        return _sharedPropertyManager;
    }
    return nil;
}

- (id) init{
    self = [super init];

    if (self != nil) {
        NSLog(@"Singleton PropertyManager is Running");

    }
    return self;
}
@end

when you need to put data from view to view you simply use

[[PropertyManager sharedPropertyManager]setChapterId:"1"];

and get it in any other view by

[[PropertyManager sharedPropertyManager]chapterId];
Mohamed Emad Hegab
  • 2,665
  • 6
  • 39
  • 64