-1

I need to create an object for example NSString and let other classes get/set the value for it.

thx. for help :)

Omarj
  • 1,151
  • 2
  • 16
  • 43

2 Answers2

4

The easiest way to do this is to attach the value as a property to a singleton instance of some class. One singleton instance that already exists in your application is the application delegate. So, just add an NSString property to your application delegate and you can access it from any class in your app (as long as you #import your application delegate).

In your application delegate:

@property(nonatomic, strong) NSString* someString;

In your other classes:

[self doSomethingWithAString:((YourAppDelegateClass*)[[UIApplication sharedApplication] delegate]).someString];
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
1

Create a Singleton/Shared class.

@implementation SINGLETON

static SINGLETON *instance = nil;

+(SINGLETON*)sharedInstance{
    @synchronized(self) {   
        if (instance == nil) {
            instance = [[SINGLETON alloc] init];
        }
        return instance;
    }
 }

EDIT:

This will come handy... Objective-C Singleton problem. Object recognized in one class but not another

Community
  • 1
  • 1
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140