derFalke
derFalke

Reputation: 247

Use NSString in another class

I simply want to access a NSString from my first class in my second class.

I used properties in my first class and this:

NSLog(@"The text is: %@", self.fileText);

returns the correct string in my first class.

But in my second class, this:

FirstViewController* controller = [[FirstViewController alloc] init];
NSLog(@"text: %@", controller.fileText);

returns (null).

I imported the class correctly. What could I have done wrong?

Upvotes: 0

Views: 310

Answers (2)

ilhan çetin
ilhan çetin

Reputation: 383

Use this

//in your first class
NSUserDefaults *strinToSave= [NSUserDefaults standardUserDefaults];
[strinToSave setObject:self.fileText forKey:@"filTextString"];

and to retrieve saved data in the second class, use this:

NSString *yourString=[[NSUserDefaults standardUserDefaults] valueForKey:@"filTextString"];
//use yourString however you want

NSLog(@"%@",yourString);

Upvotes: 2

El Developer
El Developer

Reputation: 3346

Considering the second class is either a sub-class of the first class or jus an instance of the class (which seems more likely).

Be sure what you are doing in your init block of the FirstViewController, because that's all that's being called for the second implementation and if you have not initialized your string there, the value will be null. So yep, check there.

Upvotes: 0

Related Questions