0

How can I set a global class containing sharedInstance and also how can I get and change the variable within from each viewcontroller?

The following is my code:

DataClass.h:

@interface DataClass : NSObject {
    int *toyid;
}
+(DataClass *)sharedInstance;
@end

DataClass.m:

@implementation DataClass
+(DataClass *)sharedInstance {
    static DataClass *myData = nil;
    if(nil == myData) {
        myData = [[[self class] alloc]init];
        myData.toyid=0;
    }
    return myData;
}
@end

Please help

1 Answers1

0

Your question is not quite clear. You should do more research before posting to StackOverflow.com. And choose better tags, such as Objective-C.

If you are asking to have one and only one instance of a class, doing so is a software design pattern called Singleton.

Try googling "Objective-C" and "Singleton" for examples and discussion. Such as this question on StackOverflow: What should my Objective-C singleton look like? [closed]

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154