开发者

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 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:

  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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜