0

I feel I must be going about this the wrong way and there is probably a very simple solution that I cant seem to find. In ClassA.h I have:

@property (strong, retain) NSManagedObject *form;

In ClassB I have tried:

#import "ClassA"

@interface ClassB  ()
{
    ClassA *classA;
}

Then in viewDidLoad I have:

NSLog(@"form::%@", classA.form);

but its nil everytime even though when Im on classA (its a viewcontroller) it definitely has a value.

EDIT:

So I followed the example given by BlackRider classA

 MyManager *sharedManager = [MyManager sharedManager];
    sharedManager.someProperty = self.form;

classB

MyManager *sharedManager = [MyManager sharedManager];

                    NSLog(@"globalform::%@", sharedManager.someProperty);

and Im still getting null value in the log print.

BluGeni
  • 3,378
  • 8
  • 36
  • 64

3 Answers3

1

You really should make ClassA a property of ClassB using @property (nonatomic, strong) ClassA *classA; in the interface, this will generate a setter and getter for you. Using a plain C variable like you have done means you can only access it using -> pointer indirection, and it will be protected by default. Mostly people use these kinds of variables in a class extension in their implementation, but properties on their interface.

Wherever you instantiate your ClassB class, set its classA property using classB.classA = self;

For example:

- (void)loadMyForm {
    ClassB *classB = [ClassB alloc] init];
    classB.classA = self;
}
ThisDarkTao
  • 1,062
  • 10
  • 27
0

Since you use a storyboard (and probably segues) here's what you can do:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get a reference to the destination view controller
    if([[segue destinationViewController] isKindOfClass:[ClassA class]]) {
        ClassA *vc = [segue destinationViewController];

        // now you can access vc.form
    }
}
TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
0

I ended up using my appDelegate and using it to store a "global" variable.

BluGeni
  • 3,378
  • 8
  • 36
  • 64