Please keep in mind I am new to objective c coding and do not understand many of the concepts.
I have 2 classes that must speak to one another. Class1 declares a string variable and Class2 reads it. I am not sure of the best way to go about this but after some research here is what I have.
The error is that when I access the variable in Class2 the variable is (null).
Class1.h
static NSString *var;
@interface Class1 : UIViewController {
//other variables
}
@property(nonatomic, retain) NSString *var;
@end
Class1.m
@implementation Class1
@synthesize var;
-(void)buttonAction:(UIButton*)sender;
{
var = @"some generated variable";
//after setting the variable I immediately call Class2
}
Class2.h
@interface Class2 : UIViewController {
NSString *var2;
}
@end
Class2.m
#import "Class1.h"
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
Class1 *obj = [[Class1 alloc] init];
var2 = obj.var;
NSLog(@"%@", var2);
//this print statement is (null)
}
Im just not sure where I am going wrong. If my code is unclear in any way please let me know.
Class1, you are not referencing the instance which is holding the variable. You will need to pass the instance of theClass1where you have set the variable toClass2