I’m setting a string in a view controller called ViewController and trying to access it somewhere else. This is the code:
ViewController.h
NSString *string;
...
@property (retain) NSString *string;
ViewController.m
@synthesize string;
...
-(void)viewDidLoad {
...
string = @"Test";
}
OtherViewController.m
#import "ViewController.h"
...
-(void)viewDidLoad {
ViewController *vc;
vc = [[ViewController alloc] init];
NSLog(@"String: %@", vc.string);
}
However, the log is showing: String: (null). What am I doing incorrectly? Thanks.
edit : it doesn’t necessarily need to be an NSObject class, if you want to, you could also do this on your viewController class, just be sure to also include
on your header
—- end of edit
if you’re trying to make a class that’s accessible to another view controller, why not try NSObject instead of view controller (considering you only need to take that string value)
for instance, lets call that viewController class “global” class
so at global.h, you put up
and then, at global.m you put up
after this, everytime you need to access the “myString” object that contained in global class, you could put up
at header :
at implementation file :
there you go 😉