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 valueTemp = 0;
in classB.
I popped back to clas开发者_运维百科sA from classB.
Now, I need to access Temp
value in classA.
how can I get that without setting the value in AppDelegate calss ?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 :)
I guess, there are two ways to do this:
- 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
- Create a property in App Delegate and assign this variable from Class B. When back to class A, use the variable from App Delegate.
精彩评论