srikanth rongali
srikanth rongali

Reputation: 1453

How can I access a value in a class which was set in another class in objective c?

I have two classes, classA and classB.
I navigated from classA to classB. I set a value Temp = 0; in classB.
I popped back to classA from classB.
Now, I need to access Temp value in classA.
how can I get that without setting the value in AppDelegate calss ?

Upvotes: 0

Views: 392

Answers (2)

Dipak Mishra
Dipak Mishra

Reputation: 1

I guess, there are two ways to do this:

  1. Create owner object in class B and initialize it with class A's object. When done with class B, set the value in class A's variable and then use it into class A
  2. Create a property in App Delegate and assign this variable from Class B. When back to class A, use the variable from App Delegate.

Upvotes: 0

willcodejavaforfood
willcodejavaforfood

Reputation: 44093

You should use a Property. Have a look at the apple tutorial

@interface MyClass : NSObject
{
    NSString *value;
}
@property(copy, readwrite) NSString *value;
@end



@implementation MyClass
@synthesize value;
@end

In another class that has a instance of MyClass

myInstanceOfMyClass.value = @"hi"; // Sets the value to 'hi'
NSString* myString = myInstanceOfMyClass.value; // Gets the value :)

Upvotes: 1

Related Questions