i need help to read variable from one to another class. I have followed some answers about this but with no success.
PP.h:
@interface PP : UIViewController
{
@public int fNum;
}
@property (readwrite, nonatomic) int fNum;
- (IBAction)setSomeNum;
@end
PP.m:
@synthesize fNum;
- (IBAction)setSomeNum
{
fNum = 69;
NSLog(@"Set Num Activated %d",fNum); //OK
}
TryView.m:
#import "TryView.h"
#import "PP.h"
@interface TryView ()
@end
@implementation TryView
- (void)viewDidLoad
{
[super viewDidLoad];
PP *obj ;
int x = obj.fNum;
NSLog(@"Happines %d",x); //Prints 0
}
@end
What is wrong, why it prints 0?
What you’re doing is essentially undefined behavior, so be glad it didn’t crash.
is fundamentally wrong – you are not creating/initializing the object, so
objis a dangling pointer, it can point anywere (i. e. to garbage) and crash when accessed. Even if you created the object, you should have called thesetSomeNummethod on it – it doesn’t get automagically called (why should it?). All in all, you have to write this:etc.